Reputation: 1
I have to set up my BigDecimal values at 2 places with different format.
1st One : If there are trailing zeroes then just have 1 digit after decimal and if not then just do mode as floor
eg.
1250.348 -> 1250.34
1250.0000 -> 1250.0
1250.50 -> 1250.5
So, I am trying something like this
BigDecimal value = new BigDecimal("1250.348");
value = value.setScale(2, RoundingMode.FLOOR); //I get 1250.34 which is proper
But it doesnt work for 1250.50 or 1250.000
2nd One : I just want to remove all zeroes decimal
1250.000->1250
1250.50->1250.5
Can someone tell me how can I do this ?
Upvotes: 0
Views: 76
Reputation: 111389
Use DecimalFormat. In your first case you want the format pattern 0.0#
.
import java.text.DecimalFormat;
DecimalFormat formatter = new DecimalFormat("0.0#");
formatter.setRoundingMode(RoundingMode.FLOOR);
formatter.format(new BigDecimal("1250.348"));
In the second case, you'll want to use format pattern 0.##
.
Upvotes: 1