Double vs. BigDecimal?

A BigDecimal is an exact way of representing numbers. A Double has a certain precision. Working with doubles of various magnitudes (say d1=1000.0 and d2=0.001) could result in the 0.001 being dropped alltogether when summing as the difference in magnitude is so large. With BigDecimal this would not happen. The disadvantage of BigDecimal is that … 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

How to nicely format floating numbers to string without unnecessary decimal 0’s

If the idea is to print integers stored as doubles as if they are integers, and otherwise print the doubles with the minimum necessary precision: public static String fmt(double d) { if(d == (long) d) return String.format(“%d”,(long)d); else return String.format(“%s”,d); } Produces: 232 0.18 1237875192 4.58 0 1.2345 And does not rely on string manipulation.

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

decimal vs double! – Which one should I use and when? [duplicate]

For money, always decimal. It’s why it was created. If numbers must add up correctly or balance, use decimal. This includes any financial storage or calculations, scores, or other numbers that people might do by hand. If the exact value of numbers is not important, use double for speed. This includes graphics, physics or other … Read more

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