Format BigDecimal without scientific notation with full precision

我的未来我决定 提交于 2019-12-12 07:32:14

问题


I'd like to convert a BigDecimal to String for printing purposes but print out all digits without scientific notation. For example:

BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out = d.toString(); // Or perform any formatting that needs to be done
System.out.println(out);

I'd like to get 12334535345456700.12345634534534578901 printed. Right now I get: 1.23345353454567E+16.


回答1:


To preserve the precision for a BigDecimal you need to pass the value in as a String

BigDecimal d = new BigDecimal("12334535345456700.12345634534534578901");
System.out.println(d.toPlainString());



回答2:


The BigDecimal class has a toPlainString method. Call this method instead of the standard toString and it will print out the full number without scientific notation.

Example

BigDecimal b = new BigDecimal("4930592405923095023950238502395.3259023950235902");
System.out.println(b.toPlainString());

Output: 4930592405923095023950238502395.3259023950235902



回答3:


You want to use a DecimalFormat:

DecimalFormat df = new DecimalFormat("#.#");  
String output = df .format(myBD);
System.out.println(value + " " + output);



回答4:


You could use this

BigDecimal d = BigDecimal.valueOf(12334535345456700.12345634534534578901);
String out= d.toPlainString();
System.out.println(out);


来源:https://stackoverflow.com/questions/15834879/format-bigdecimal-without-scientific-notation-with-full-precision

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!