首页 > 其他 > 详细

DecimalFormat格式化输出带小数的数字类型

时间:2017-07-01 09:39:20      阅读:315      评论:0      收藏:0      [点我收藏+]

刚開始

double d = 333333333.333333333;
System.out.println(d);
输出结果为3.333333333333333E8

网上找到了DecimalFormat对象

double d = 333333333.333333333;
DecimalFormat decimalFormat = new DecimalFormat("#.#");
System.out.println(decimalFormat.format(d));
结果为333333333.3,发现假设要输出后面的小数,就须要把字符串改为"#.####",没添加一个#就添加一位小数

我认为这个非常麻烦,看了下帮助文档,发现有Max值。试了下输出

DecimalFormat decimalFormat = new DecimalFormat("#.#");
System.out.println("Integer:"+decimalFormat.getMaximumIntegerDigits()+"\nFraction:"+decimalFormat.getMaximumFractionDigits());
			

结果为

Integer:2147483647
Fraction:0

看来是我版本号默认的小数位数值。太少了,所以改动下值

double d = 333333333.333333333;
DecimalFormat decimalFormat = new DecimalFormat("#.#");
//decimalFormat.setMaximumIntegerDigits(100);//显示整数的最大字数,默认值足够大
decimalFormat.setMaximumFractionDigits(1000);//显示小时位的最大字数
System.out.println("Integer:"+decimalFormat.getMaximumIntegerDigits()+"\nFraction:"+decimalFormat.getMaximumFractionDigits());
System.out.println(decimalFormat.format(d));


输出结果为

Integer:2147483647
Fraction:1000

333333333.3333333

最终符合要求了












DecimalFormat格式化输出带小数的数字类型

原文:http://www.cnblogs.com/blfbuaa/p/7101262.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!