Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java BigDecimal remove decimal and trailing numbers

Tags:

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.

like image 506
Zero Cool Avatar asked Aug 22 '09 20:08

Zero Cool


People also ask

How do I remove trailing zeros from BigDecimal?

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.

How do you exclude decimals in Java?

int x = (int) y where y is your double variable. Then, printing x does not give decimal places (15000 instead of 15000.0).

How do I round off BigDecimal?

BigDecimal. round(MathContext mc) returns a BigDecimal rounded according to the MathContext settings. If the precision setting is 0 then no rounding takes place.


2 Answers

Use this.

BigDecimal truncated= number.setScale(0,BigDecimal.ROUND_DOWN); 
like image 68
S.Lott Avatar answered Sep 16 '22 16:09

S.Lott


Try number.toBigInteger().toString()

like image 26
David Z Avatar answered Sep 17 '22 16:09

David Z