java中Double转为String时不以科学计数法显示
程序员文章站
2022-07-13 21:22:04
...
问题:
当Double的长度超过一定长度的时候,会以科学计数法显示。对于前段显示不是很友好。因此需要将其转化为原样式显示。
下面以前端显示金额为例,保留2位小数
-
最佳方法
Double num = 123000000.456d;//1.23000000456E8 //按照原样 String str = new BigDecimal(Double.toString(num)).toPlainString();//123000000.456 //按照四舍五入方式保留2位小数 String str2 = new BigDecimal(Double.toString(num)).setScale(2, RoundingMode.HALF_UP).toString();//123000000.46
-
分析过程
public class DoubleTest { private static void doubleTest1() { Double d = 123.000; System.out.println(d.toString());//123.0 System.out.println(d.longValue());//123 System.out.println(String.valueOf(d));//123.0 System.out.println(Double.toString(d));//123.0 System.out.println(new BigDecimal(d));//123 System.out.println(new BigDecimal(Double.toString(d)).toString());//123.0 System.out.println(new BigDecimal(Double.toString(d)).setScale(2, RoundingMode.HALF_UP).toString());//123.00 System.out.println(new DecimalFormat("0.00").format(d));//123.00 System.out.println(doubleToString(d));//123 Double d2 = 123.456; System.out.println(d2.toString());//123.456 System.out.println(d2.longValue());//123 System.out.println(String.valueOf(d2));//123.456 System.out.println(Double.toString(d2));//123.456 System.out.println(new BigDecimal(d2));//123.4560000000000030695446184836328029632568359375 System.out.println(new BigDecimal(Double.toString(d2)).toString());//123.456 System.out.println(new BigDecimal(Double.toString(d2)).setScale(2, RoundingMode.HALF_UP).toString());//123.46 System.out.println(new DecimalFormat("0.00").format(d2));//123.46 System.out.println(doubleToString(d2));//123.46 } private static void doubleTest2() { Double d = 123000000000.000; System.out.println(d.toString());//1.23E11 System.out.println(d.longValue());//123000000000 System.out.println(String.valueOf(d));//1.23E11 System.out.println(Double.toString(d));//1.23E11 System.out.println(new BigDecimal(d));//123000000000 System.out.println(new BigDecimal(d).setScale(2));//123000000000.00 System.out.println(new BigDecimal(Double.toString(d)).toString());//1.23E+11 System.out.println(new BigDecimal(Double.toString(d)).setScale(2, RoundingMode.HALF_UP).toString());//123000000000.00 System.out.println(new DecimalFormat("0.00").format(d));//123000000000.00 System.out.println(doubleToString(d));//123000000000 Double d2 = 123000000000.456; System.out.println(d2.toString());//1.23000000000456E11 System.out.println(d2.longValue());//123000000000 System.out.println(String.valueOf(d2));//1.23000000000456E11 System.out.println(Double.toString(d2));//1.23000000000456E11 System.out.println(new BigDecimal(d2));//123000000000.45599365234375 System.out.println(new BigDecimal(d).setScale(2).toString());//123000000000.00 System.out.println(new BigDecimal(Double.toString(d2)).toString());//123000000000.456 System.out.println(new BigDecimal(Double.toString(d2)).setScale(2, RoundingMode.HALF_UP).toString());//123000000000.46 System.out.println(new DecimalFormat("0.00").format(d2));//123000000000.46 System.out.println(doubleToString(d2));//123000000000.46 } /** * doubleToString */ private static String doubleToString(Double d) { NumberFormat nf = NumberFormat.getInstance(); //四舍五入 nf.setRoundingMode(RoundingMode.HALF_UP); //取消遇到3位就加',',如1230,表示为1,230 nf.setGroupingUsed(false); //最大小数位 nf.setMaximumFractionDigits(2); return nf.format(d); } }
上一篇: jq中自定义 验证信息
下一篇: 函数