Java BigDecimal valueOf not precise? [duplicate] - java

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.

Related

How to use a large number in Java like 300 decimal digits or more [duplicate]

This question already has answers here:
Large Numbers in Java
(6 answers)
Closed 1 year ago.
I declared a long in Java code and it only take 19 decimal digits. I need a data type that takes over 300 decimal digits.
There is no primitive type that supports a number that long.
What you are searching for is most likely a BigInteger object. Calculations are done through the methods shown in the API.
As another user said, Javascript and Java are two different languages, please use the correct tag.

why is the output of System.out.println((5/2.0)*2); is 5.0? [duplicate]

This question already has answers here:
Integer division: How do you produce a double?
(11 answers)
Java integer-double division confusion [duplicate]
(5 answers)
Closed 4 years ago.
I think 5 is an int. But there are no variables. I don't know why the output is a double. I tried in Netbeans already.
Multiplication and division are done left to right (and regardless, you put parentheses around the first operation), and if you divide any primitive umber by a double, you will get a double out of it.
5/2.0 will clearly be evaluated first.
So, even though ‘5’ is an int, if you do 5/2.0, you’ll get a double (namely, 2.5). Now, at this point, your code is already working with a double.
When you multiply 2.5 by 2, you then output 5.0, since 2.5 is a double.

Convert double to BigInteger [duplicate]

This question already has answers here:
is there anyway to convert from Double to BigInteger?
(3 answers)
Closed 5 years ago.
How do you convert a double to a BigInteger, as accurately as possible? BigInteger doesn't have a valueOf(double) method. It does have a valueOf(long) method, but converting via long, would destroy any value outside the 2^64 range, and in particular I'm trying to get large numbers like 1e100 to convert to their corresponding integer values to the sixteen or so significant figures allowed by floating point precision.
You can convert the double into a BigDecimal and then into a BigInteger:
BigInteger k = BigDecimal.valueOf(doublevalue).toBigInteger();
You could use BigDecimal.valueOf(double) and then call toBigIntegerExact() on that. Like,
BigInteger bi = BigDecimal.valueOf(Math.PI).toBigIntegerExact();

BigDecimal how to use? [duplicate]

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.

How to format BigDecimal to ALWAYS have two decimal places? [duplicate]

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)

Categories

Resources