What pattern of DecimalFormat should I use so that when I format BigDecimals like
new BigDecimal("4235886589.00000");
new BigDecimal("4235886589.0000030000");
new BigDecimal("4235886589.0");
new BigDecimal("4235886589");
The output must have the following appearance:
4,235,886,589.00000; 4,235,886,589.0000030000; 4,235,886,589.0; 4,235,886,589;
So basically I'm trying to write a pattern which won't cut off zeros in the end and won't put them if they are not necessary. I tried to handle it with patterns:
DecimalFormat formatter = new DecimalFormat("#,###,##0.########");
DecimalFormat formatter = new DecimalFormat("#,###,##0.000000");
But the first formatter cuts off zeros, whereas the second one puts them where they are unnecessary.
I don't think you can do it in a straight way using the formatter. What you can do is wrap your Decimal in a class where you can store the appropriate format string computed at instantiation:
class MyBigDecimal {
String asString;
BigDecimal n;
public MyBigDecimal(String asString) {
this.asString = asString;
n = new BigDecimal(asString);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.