rounding

Why does str() round up floats?

♀尐吖头ヾ 提交于 2019-12-17 20:53:48
问题 The built-in Python str() function outputs some weird results when passing in floats with many decimals. This is what happens: >>> str(19.9999999999999999) >>> '20.0' I'm expecting to get: >>> '19.9999999999999999' Does anyone know why? and maybe workaround it? Thanks! 回答1: It's not str() that rounds, it's the fact that you're using floats in the first place. Float types are fast, but have limited precision; in other words, they are imprecise by design . This applies to all programming

Java rounding to nearest 0.05

北慕城南 提交于 2019-12-17 20:46:26
问题 I am trying to find a way to round values to the nearest 0.05. For example: 0.93 rounds to 0.95 0.81 rounds to 0.80 0.65 stays 0.65 0.68 to 0.70 0.67 to 0.65 Is there a simple way to do this in Java? 回答1: One option for doing this would be as follows: Multiply the value by 20. Use Math.round to round to the nearest integer. Divide by 20 again. For example: double rounded = Math.round(x * 20.0) / 20.0; Hope this helps! 来源: https://stackoverflow.com/questions/9256005/java-rounding-to-nearest-0

How to eliminate the extra minus sign when rounding negative numbers towards zero in numpy?

核能气质少年 提交于 2019-12-17 19:14:55
问题 I have a simple question about the fix and floor functions in numpy . When rounding negative numbers that are larger than -1 towards zero, numpy round them off correctly to zero however leaves a negative sign. This negative sign interferes with my costume unique_rows function since it uses the ascontiguousarray to compare elements of the array and this sign disturbs the uniqueness. Both round and fix behave the same in this regard. >>> np.fix(-1e-6) Out[1]: array(-0.0) >>> np.round(-1e-6) Out

Rounding NSDecimalNumber

旧时模样 提交于 2019-12-17 19:11:04
问题 I'm having the hardest time figuring out something that seems like it should be very simple. I need to accurately round an NSDecimalNumber to a particular number of decimal places (determined at runtime.) So far as I can tell, I have two options, neither of which I like. Convert to a float, and use C rounding functions: I don't like this because accuracy matters in this case. Floats can't always accurately represent decimal numbers, and this could cause problems. Convert to a string using

Rounding NSDecimalNumber

懵懂的女人 提交于 2019-12-17 19:09:03
问题 I'm having the hardest time figuring out something that seems like it should be very simple. I need to accurately round an NSDecimalNumber to a particular number of decimal places (determined at runtime.) So far as I can tell, I have two options, neither of which I like. Convert to a float, and use C rounding functions: I don't like this because accuracy matters in this case. Floats can't always accurately represent decimal numbers, and this could cause problems. Convert to a string using

round to nearest .25 javascript

孤街浪徒 提交于 2019-12-17 18:04:29
问题 I want to convert all numbers to the nearest .25 So... 5 becomes 5.00 2.25 becomes 2.25 4 becomes 4.00 3.5 becomes 3.50 Thanks 回答1: Here’s an implementation of what rslite said: var number = 5.12345; number = (Math.round(number * 4) / 4).toFixed(2); 回答2: Multiply by 4, round to integer, divide by 4 and format with two decimals. Edit Any reason for the downvotes? At least leave a comment to know what should be improved. 回答3: If speed is your concern, note that you can get about a 30% speed

How do I round a float up to the nearest int in C#?

∥☆過路亽.° 提交于 2019-12-17 17:55:09
问题 In C#, how do I round a float to the nearest int? I see Math.Ceiling and Math.Round, but these returns a decimal. Do I use one of these then cast to an Int? 回答1: If you want to round to the nearest int: int rounded = (int)Math.Round(precise, 0); You can also use: int rounded = Convert.ToInt32(precise); Which will use Math.Round(x, 0); to round and cast for you. It looks neater but is slightly less clear IMO. If you want to round up : int roundedUp = (int)Math.Ceiling(precise); 回答2: Off the

How might I convert a double to the nearest integer value?

旧城冷巷雨未停 提交于 2019-12-17 17:52:54
问题 How do you convert a double into the nearest int? 回答1: Use Math.round() , possibly in conjunction with MidpointRounding.AwayFromZero eg: Math.Round(1.2) ==> 1 Math.Round(1.5) ==> 2 Math.Round(2.5) ==> 2 Math.Round(2.5, MidpointRounding.AwayFromZero) ==> 3 回答2: double d = 1.234; int i = Convert.ToInt32(d); Reference Handles rounding like so: rounded to the nearest 32-bit signed integer. If value is halfway between two whole numbers, the even number is returned; that is, 4.5 is converted to 4,

Android - Round to 2 decimal places [duplicate]

孤人 提交于 2019-12-17 17:27:14
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Round a double to 2 significant figures after decimal point I know that there are plenty of examples on how to round this kind numbers. But could someone show me how to round double, to get value that I can display as a String and ALWAYS have 2 decimal places? 回答1: You can use String.format("%.2f", d) , your double will be rounded automatically. 回答2: One easy way to do it: Double d; Int i; D+=0.005; i=d*100;

How to specify RoundingMode for decimal numbers in Jasper Reports

我的未来我决定 提交于 2019-12-17 17:05:04
问题 I'm using Java with Jasper Reports and would like to format a decimal value using this format mask "#,##0.00" . At the first sight all looks fine, but I found that my decimal values are rounded using RoundingMode.HALF_EVEN rounding mode and this is not correct in my case. Is this possible to specify an other rounding mode (I need HALF_DOWN mode)? 回答1: You can use the scriptlets mechanism . The sample Java class package utils; import java.math.BigDecimal; import java.math.RoundingMode; import