rounding

Round currency closest to five

≯℡__Kan透↙ 提交于 2020-01-21 11:55:06
问题 I'd like to round my values to the closest of 5 cent for example: 5.31 -> 5.30 5.35 -> 5.35 5.33 -> 5.35 5.38 -> 5.40 Currently I'm doing it by getting the decimal values using: let numbers = 5.33 let decimal = (numbers - rint(numbers)) * 100 let rounded = rint(numbers) + (5 * round(decimal / 5)) / 100 // This results in 5.35 I was wondering if there's a better method with fewer steps because sometimes numbers - rint(numbers) is giving me a weird result like: let numbers = 12.12 let decimal =

How to round percentage to 2 decimal places in ggplot2

只谈情不闲聊 提交于 2020-01-20 08:20:51
问题 My code produces this graph: Code: ggplot(areas, aes(x = RETAILER, y = Difference), label=sprintf("%0.2f", round(areas$Difference, digits = 2))) + geom_bar(stat = "identity", aes(fill = RETAILER), colour = "black") + scale_y_continuous("Percentage", labels = percent_format()) + geom_text(aes(label = paste(Difference * 100, "%"), vjust = ifelse(Difference >= 0, -1.5, 1.5))) + theme_classic() I have found code to allow the data labels to be positioned correctly above or below each bar, but I

R issue with rounding milliseconds

谁都会走 提交于 2020-01-20 05:00:19
问题 Given the following issue with rounding milliseconds under R. How do I get around it so that the times are correct? > options(digits.secs=3) > as.POSIXlt("13:29:56.061", format='%H:%M:%OS', tz='UTC') [1] "2012-06-07 13:29:56.060 UTC" > as.POSIXlt("13:29:56.062", format='%H:%M:%OS', tz='UTC') [1] "2012-06-07 13:29:56.061 UTC" > as.POSIXlt("13:29:56.063", format='%H:%M:%OS', tz='UTC') [1] "2012-06-07 13:29:56.063 UTC" I noticed that this URL provides background information but doesn't solve my

C printf float rounding

拜拜、爱过 提交于 2020-01-17 04:14:28
问题 I have to reimplement printf(3) with C without using any function that would do the conversion for me. I'm nearly done I just need %a and it's also nearly done thanks to you guys : How %a conversion work in printf statement? The man says: The double argument is rounded and converted to hexadecimal notation in the style[-]0xh.hhhp[+-]d, where the number of digits after the hexadecimal-point character is equal to the precision specification. So my question is rounded how ? I found: printf

MySQL TRUNCATE or ROUND with variable decimal length

[亡魂溺海] 提交于 2020-01-17 01:30:28
问题 I am not able to TRUNCATE (or round) a decimal on variable decimal lengh . Here's my MySQL query: SELECT TRUNCATE( `amount` , `decimal_length` ) AS truncated_decimal, FROM table -- sample data amount decimal length 123.123 0 456.456 1 789.789 2 --expected outcome truncated_decimal 123 456.4 789.78 --current outcome (wrong) truncated_decimal 123.123 456.456 789.789 My truncated_decimal variable is returned WITHOUT truncation, i.e. default decimal length from table. Same behavior with ROUND

Java - Round number up so number of digits increments

≡放荡痞女 提交于 2020-01-15 11:26:36
问题 What is the most efficient way (in Java) to round a number n up to the nearest power of ten which contains one more digit than the original number? e.g. 3 -> 10 432 -> 1,000 241,345 -> 1,000,000 Is there a way to get it in a single O(1) line? A simple way I can see is to use a for loop and increment the power of ten until n / (10 ^ i) < 1, but then that isn't O(1) and is O(log n) instead. (well I'm taking a guess it's log n as it involves a power!) 回答1: If you're looking for a string, you can

Default CUDA addition rounding mode between cuda 5.0 and 7.5

本小妞迷上赌 提交于 2020-01-15 05:38:06
问题 I have a cuda loop where a variable cumul store an accumulation in double : double cumulative_value = (double)0; loop(...) { // ... double valueY = computeValueY(); // ... cumulative_value += valueY } This code is compiled on different SDK and run on two computers : M1 : TeslaM2075 CUDA 5.0 M2 : TeslaM2075 CUDA 7.5 At step 10, results are differents. Values for this addition (double precision representation in hexadecimal) are: 0x 41 0d d3 17 34 79 27 4d => cumulative_value + 0x 40 b6 60 1d

How to maintain Double's precision in C++

巧了我就是萌 提交于 2020-01-14 03:15:29
问题 main() { double d1 = 1234.1; cout << "d1 = 1234.1 --> " << d1 << endl; double d2 = 1234.099999; cout << "d2 = 1234.099999 --> " << d2 << endl; } Output: d1 = 1234.1 --> 1234.1 d2 = 1234.099999 --> 1234.1 I actually want to print the exact value of d2 , i.e. 1234.099999 but not getting the same. Please suggest how can I get the exact value. 回答1: You want cout.precision http://www.cplusplus.com/reference/iostream/ios_base/precision/ Also note that d2 is not quite 1234.099999, and d1 is not

Q: How to ROUNDUP a number in Access 2013?

送分小仙女□ 提交于 2020-01-14 03:07:26
问题 For Access 2013, I need a way to round up any fractional numbers to the next whole number in an SQL query. Example: SELECT ROUNDUP(NumberValues) FROM Table1 In the above query, a row with 1.25 should return as 2 after querying. As far as I know, there's no ROUNDUP function in Access 2013 for use in a SQL query statement. 回答1: I found a ROUNDUP equivalent from this link: http://allenbrowne.com/round.html#RoundUp To round upwards towards the next highest number, take advantage of the way Int()

Evaluate 1/tanh(x) - 1/x for very small x

廉价感情. 提交于 2020-01-13 13:12:20
问题 I need to compute the quantity 1/tanh(x) - 1/x for x > 0 , where x can be both very small and very large. Asymptotically for small x , we have 1/tanh(x) - 1/x -> x / 3 and for large x 1/tanh(x) - 1/x -> 1 Anyhow, when computing the expression, already from 10^-7 and smaller round-off errors lead to the expression being evaluated as exactly 0: import numpy import matplotlib.pyplot as plt x = numpy.array([2**k for k in range(-30, 30)]) y = 1.0 / numpy.tanh(x) - 1.0 / x plt.loglog(x, y) plt.show