C
ChrisMM
I have a double that I need to round to 6 dp, I thought I could just
assign it to a BigDecimal, call its scale method and assign the result
back to the double variable but something weird happens :-
This code ...
double db = 0.1234567890;
System.out.print(" Double = "+ db );
BigDecimal big = new BigDecimal( db );
System.out.print(" big = "+big);
System.out.print(" rounded big = "+big.setScale (6,
BigDecimal.ROUND_HALF_UP));
System.out.println(" back to double = " + new Double(
big.doubleValue() ));
produces this output...
Double = 0.12345678900000004
big = 0.1234567890000000389694179148136754520237445831298828125
rounded big = 0.123457
back to double = 0.12345678900000004
The double somehow goes back to its original value ???
Whats happening ? and how do I get the double to be the rounded
amount.
Cheers,
ChrisMM
assign it to a BigDecimal, call its scale method and assign the result
back to the double variable but something weird happens :-
This code ...
double db = 0.1234567890;
System.out.print(" Double = "+ db );
BigDecimal big = new BigDecimal( db );
System.out.print(" big = "+big);
System.out.print(" rounded big = "+big.setScale (6,
BigDecimal.ROUND_HALF_UP));
System.out.println(" back to double = " + new Double(
big.doubleValue() ));
produces this output...
Double = 0.12345678900000004
big = 0.1234567890000000389694179148136754520237445831298828125
rounded big = 0.123457
back to double = 0.12345678900000004
The double somehow goes back to its original value ???
Whats happening ? and how do I get the double to be the rounded
amount.
Cheers,
ChrisMM