最近,在做项目的时候,遇到了科学技术法转字符串的问题,网上搜了一下,解决如下:
最近,在做项目的时候,遇到了科学技术法转字符串的问题,网上搜了一下,解决如下:
public class T {
public static void main(String[] args) { System.out.println("科学计数法数字"); double num1 = 50123.12E25; System.out.println(num1); BigDecimal bd1 = new BigDecimal(num1); System.out.println(bd1.toPlainString()); System.out.println(bd1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); System.out.println("普通数字"); double num2 = 50123.12; System.out.println(num2); BigDecimal bd2 = new BigDecimal(num2); System.out.println(bd2.toPlainString()); System.out.println(bd2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString()); }
}
结果为:
科学计数法数字
5.012312E29
501231200000000030561980645376
501231200000000030561980645376.00
普通数字
50123.12
50123.1200000000026193447411060333251953125
50123.12
亲测,有效