-1

Is it possible to store java.lang.Double to exactly two decimal places Ex : 0.2 becomes 0.20

I modified the code to following

            Double correlation = getSomeDoubleValue(label, key, date);
            DecimalFormat df = new DecimalFormat("0.00");
            correlation = Double.valueOf(df.format(correlation));

but when function getSomeDoubleValue() returns 0.2 , df.format(correlation) makes it 0.20 but as i need java.lang.Double , then Double.valueOf("0.20") gives me 0.2 back.

I am not sure how to achieve this or if its achievable at all.

gkrls
  • 2,618
  • 2
  • 15
  • 29
Lav
  • 1,283
  • 5
  • 21
  • 48
  • http://stackoverflow.com/questions/2808535/round-a-double-to-2-decimal-places – JNL Aug 19 '14 at 14:25
  • Thanks ... but rounding is not what i am looking for ... as title suggests STORING , not sure why a minus 1 for this – Lav Aug 19 '14 at 14:30
  • You can't change the internal representation of doubles. Every double is stored as `m*2^e` (simplified). `0.2`, `0.20`, `0.200`, `2E-1`, `.2` are all the same double but different `String` representations. Normally you would just do your calculations with doubles and only round on printing. – Absurd-Mind Aug 19 '14 at 14:44
  • Thanks , i had a similar opinion was looking for confirmation – Lav Aug 19 '14 at 14:45
  • also I am not sure, if you only want this double number rounded to 2 decimal places and just want to use it as double for further calculations and don't want to print it, then how does it matter 0.20 or 0.2 both are same in mathematics right ? for at the time of printing it you can use System.out.printf() or String.format(). – Manoj Aug 19 '14 at 15:29
  • It may help if you explain how you intend to use this. I find the question confusing, because double is a radix-2 floating point format with, for normal range numbers, 53 significant bit precision. Concepts like "2 decimal places" just don't make sense for it. – Patricia Shanahan Aug 20 '14 at 01:18

5 Answers5

1

Floating point numbers, such as double and float are really mathematical expressions and are inexact because of it.

If you want something more exact, you should use the BigDecimal class, which is an arbitrary precision decimal number.

You can set the number of places a BigDecimal stores by setting its scale using one of its setScale functions, which can also be used to set which rounding mode it uses when you do math operations on it.

Be aware that BigDecimal is an object type, so you need to use its methods to do math operations with it (.add, .subtract, .multiply, .divide).

BigDecimal has constructors for creating a BigDecimal from a double, long, or int.

Powerlord
  • 87,612
  • 17
  • 125
  • 175
  • Floating point _numbers_ are not inexact. Every floating point number is exactly what it is. Floating point _operations_ produce inexact results whenever the exact result has no floating point representation. Don't forget, when you write `0.2` that's a floating point _operation_, because "0.2" is not a float, it's a string that the compiler has to parse and turn into a floating point number. – Solomon Slow Sep 18 '14 at 15:18
  • @jameslarge Floating point numbers are inexact from a usability standpoint... People don't usually want 0.199999999997 either when they type in 0.2 or do operations that should be around 0.2. Floating point rounding issues are [well known and documented](http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html). There's a reason why most programming languages include some sort of decimal library, even if it does use different semantics like Java's `BigDecimal` does. – Powerlord Sep 18 '14 at 15:35
  • It looks like I mis-read your reply. I thought you said, "floating point numbers are inexact." People who say that instead of saying that floating point _operations_ produce inexact results often have a weak understanding of what is really going on. But looking back at your reply, I see that that's not what you said at all. My mistake. Sorry. – Solomon Slow Sep 18 '14 at 17:16
0

A double with a value of 0.2 is storing the fact that the number is 0.20. In addition, it also represents the number 0.200, 0.2000, 0.20000, etc. What it sounds like you're looking for is a way to print out numbers to two decimal places of precision. To do this, see: Best way to Format a Double value to 2 Decimal places

Community
  • 1
  • 1
Alex Kleiman
  • 709
  • 5
  • 14
0

Is this for storing money?

If so, use int or long and store the total number of cents. Then just move the decimal point when needed.

So instead of storing 0.20 you store 20 and when printing know to put the decimal point.

dkatzel
  • 31,188
  • 3
  • 63
  • 67
0

one way to get 0.2 as 0.20 is to store the value in String. e.g. String number = String.format("%.2f",correlation );

Manoj
  • 814
  • 8
  • 7
0

Your problem is not about precision. By default doubles in Java have a fixed precision which is far greater than 2 decimal places. I.E. the value is stored with more than 2 decimal places by default.
You can also take a look at this question
It seems to me that you are looking for a way to represent only 2 decimal digits. One way is by using printf

For example:

System.out.printf("%.2f", 0.2);

This will print:

0.20

Hope this helps

Community
  • 1
  • 1
gkrls
  • 2,618
  • 2
  • 15
  • 29