Why does Math.Round(2.5) return 2 instead of 3?

Firstly, this wouldn’t be a C# bug anyway – it would be a .NET bug. C# is the language – it doesn’t decide how Math.Round is implemented. And secondly, no – if you read the docs, you’ll see that the default rounding is “round to even” (banker’s rounding): Return ValueType: System.DoubleThe integer nearest a. If … Read more

Round a double to 2 decimal places [duplicate]

Here’s an utility that rounds (instead of truncating) a double to specified number of decimal places. For example: round(200.3456, 2); // returns 200.35 Original version; watch out with this public static double round(double value, int places) { if (places < 0) throw new IllegalArgumentException(); long factor = (long) Math.pow(10, places); value = value * factor; … Read more

Why does Math.round(0.49999999999999994) return 1?

Summary In Java 6 (and presumably earlier), round(x) is implemented as floor(x+0.5).1 This is a specification bug, for precisely this one pathological case.2 Java 7 no longer mandates this broken implementation.3 The problem 0.5+0.49999999999999994 is exactly 1 in double precision: static void print(double d) { System.out.printf(“%016x\n”, Double.doubleToLongBits(d)); } public static void main(String args[]) { double … Read more

Formatting a number with exactly two decimals in JavaScript

To format a number using fixed-point notation, you can simply use the toFixed method: (10.8).toFixed(2); // “10.80” var num = 2.4; alert(num.toFixed(2)); // “2.40” Note that toFixed() returns a string. IMPORTANT: Note that toFixed does not round 90% of the time, it will return the rounded value, but for many cases, it doesn’t work. For … Read more

How to round a number to n decimal places in Java

Use setRoundingMode, set the RoundingMode explicitly to handle your issue with the half-even round, then use the format pattern for your required output. Example: DecimalFormat df = new DecimalFormat(“#.####”); df.setRoundingMode(RoundingMode.CEILING); for (Number n : Arrays.asList(12, 123.12345, 0.23, 0.1, 2341234.212431324)) { Double d = n.doubleValue(); System.out.println(df.format(d)); } gives the output: 12 123.1235 0.23 0.1 2341234.2125 EDIT: … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)