<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
NumberFormat用于格式化数据或者解析数字字符串。这两天在考虑如何将一个数字字符串转化为指定位数的小数格式,具体的是float类型。
输出为
11.035, 11.035, 11.0, 11.035, 11.0, 11.035
12.035, 12.035, 12, 12.035, 12.0, 12.035
可以看出要想得到想要的结果必须采用nf.parse(nf.format(d)).floatValue() 方式。也就是说在用parse方法的时候,设置的小数位数并没有用,只有在format的时候才会用。注意的是,如果要想没有小数的float是不可能的,除非要设置成int。nf.setRoundingMode(RoundingMode.HALF_UP)则设置四舍五入。
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>