This question already has answers here:
BigDecimal - to use new or valueOf
(4 answers)
Closed 5 years ago.
Lets say ItemImpl takes in "int ItemId, String name, BigDecimal price"
How do I put a BigDecimal number in the input of this?
Item item = new ItemImpl(5, "Hockey Stick", BigDecimal(1.5));
Also what's the point of BigDecimal?
Item item = new ItemImpl(5, "Hockey Stick", new BigDecimal("1.5"));
Best practice is to use a String due to precision issues
And this link I think answers pretty much everything you might want to know:
http://www.opentaps.org/docs/index.php/How_to_Use_Java_BigDecimal:_A_Tutorial
The point of BigDecimal is that it offers better precision than double and it also solves round-off errors.
Related
This question already has answers here:
BigDecimal not keeping actual value when being returned from Java method
(2 answers)
Closed 12 months ago.
I am not sure if I am using this wrong but, if I do BigDecimal.valueOf(15350.00), why does it show as 15350.0 instead of 15350.00?
When working with BigDecimal, prefer using the BigDecimal(String) constructor over valueOf(double). new BigDecimal("15350.00") will preserve your trailing zeroes.
Although 15350.00 and 15350.0 are two different BigDecimal values, BigDecimal.valueOf(15350.00) is constructing the BigDecimal using a Double. The double literals 15350.00 and 15350.0 both correspond to the same exact integer value, without the precision semantics of BigDecimal.
This question already has answers here:
Why does integer division code give the wrong answer? [duplicate]
(4 answers)
Closed 6 years ago.
I am doing simple calculation in java. Expected result is 51.3348 but what I am getting is 51.0, here is my calculation
float percent = (7819140000l-3805200000l)*100/7819140000l;
Is that problem with datatype? How can I resolve this to get value as 51.3348
Thanks in Advance
add an f to one of the values:
float percent = (7819140000l-3805200000l)*100f/7819140000l;
if yiu do not do it, Java will make a devision by long values
This question already has answers here:
How do I print a double value without scientific notation using Java?
(18 answers)
Closed 6 years ago.
I need to convert string value to double with dot. Here is simple code
double dValue=Double.parseDouble("999999999.99");
System.out.println(dValue);
output is: 9.9999999999E8
When i gave value like 10000 or 100000 it works. Help me to overcome this problem.
You could use BigDecimal and toPlainString() for that.
BigDecimal dValue= new BigDecimal("999999999.99");
System.out.println(dValue.toPlainString());
Output:
999999999.99
You can use String.format
System.out.println(String.format("%.2f", dValue));
This question already has answers here:
How can I handle precision error with float in Java?
(9 answers)
Closed 8 years ago.
some calculations with doubles return the wrong result.
E.g.
System.out.println(""+(0.05+0.01));
output
0.060000000000000005
What can I do to correct this error?
Doubles are not made for precise calculations (see Round to 2 decimal places) - for precise calculations, use BigDecimal instead.
This question already has answers here:
Java BigDecimal remove decimal and trailing numbers
(6 answers)
Java BigDecimal bugs with String constructor to rounding with ROUND_HALF_UP
(3 answers)
Closed 9 years ago.
I'm using BigDecimal to deal with positive numbers in my app, and I want to make it truncate decimals after the 4th one.
I seem to almost find what I wanted with RoundingMode.DOWN, but there's some issue when the BigDecimal is created from a double.
For example:
System.out.println("123.11119 = " + new BigDecimal("123.11119").setScale(4, RoundingMode.DOWN)); //Print 123.1111 which is exactly what I want
System.out.println("123.1111 = " + new BigDecimal("123.1111").setScale(4, RoundingMode.DOWN)); //So does this one
HOWEVER, the following prints 123.1110 which is NOT what I want at all:
System.out.println("123.1111 = " + new BigDecimal(123.1111d).setScale(4, RoundingMode.DOWN));
This is because of representation problem.
The value 123.1111d in reality can be something like 123.111091231918498.
That why it is recommended to use string constructor when you want to have exact values.