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.
Related
This question already has answers here:
Beginners Java Question (int, float)
(4 answers)
Division of integers in Java [duplicate]
(7 answers)
Closed 2 years ago.
If I divide a double type variable, the Decimal part becomes zero.
a=13122/10;
System.out.println (a);
Prints
1312.0
As you can see, the Decimal part became zero when I divided it.
But I need the value
1312.2
Your problem is that while you may have stored "a" as a double, you are really dividing two "ints" and saving that. When you divide 2 ints, the number automatically gets rounded down. So, it's rounded down to 1312.0.
What you need is this,
a = (double)13122/10;
or this:
a = 13122.0/10;
You are dividing integers. You can cast the division and then you are ok
double a = (double) 13122/10;
System.out.println (a);
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 3 years ago.
I want to take a float that can be entered by the user to any acceptable number of decimal places and format it so that it is only two decimal places. I am able to do this by converting it to a String but I want to keep it as a float and was wondering if there was an easier way to do this than changing from float to string and then back to a float?
Here's a way to do it:
https://stackoverflow.com/a/153785/4119650
It's a little more complicated than the String conversion method, but it gives you greater control over the truncation.
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.
This question already has an answer here:
Rounding BigDecimal to *always* have two decimal places
(1 answer)
Closed 6 years ago.
This:
BigDecimal.valueOf(0.00)
becomes 0.0
I want it to be 0.00
What is the correct format pattern for that?
Use the String constructor: new BigDecimal("0.00"). Using BigDecimal.valueOf(double) completely destroys any formatting you used to input the value.
Try this
value = value.setScale(2, RoundingMode.CEILING)
This question already has answers here:
Java BigDecimal remove decimal and trailing numbers
(6 answers)
Closed 6 years ago.
I have a BigDecimal number, I just want to remove the decimal numbers from it,
for example if I have 200.88 then output should be 200?
I tried Bigdecimal rounding function but they wont do the job
You can specify the rounding mode to ROUND_FLOOR when you use round.