I'm new to Java and trying to take a BigDecimal (for example 99999999.99) and convert it to a string but without the decimal place and trailing numbers. Also, I don't want commas in the number and rounding is not needed.
I've tried:
Math.Truncate(number)
but BigDecimal is not supported.
Any ideas?
Thanks very much.
stripTrailingZeros() is an inbuilt method in Java that returns a BigDecimal which is numerically equal to this one but with any trailing zeros removed from the representation. So basically the function trims off the trailing zero from the BigDecimal value.
int x = (int) y where y is your double variable. Then, printing x does not give decimal places (15000 instead of 15000.0).
BigDecimal. round(MathContext mc) returns a BigDecimal rounded according to the MathContext settings. If the precision setting is 0 then no rounding takes place.
Use this.
BigDecimal truncated= number.setScale(0,BigDecimal.ROUND_DOWN);
Try number.toBigInteger().toString()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With