码迷,mamicode.com
首页 > 其他好文 > 详细

float保留指定位数的小数

时间:2016-04-27 01:35:36      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

两种方法(主要是要注意返回值类型):

方法一

返回一个float类型

        float num = 1.23456f;
        float num2 = (float)(Math.round(num*100))/100;  //100指的是要保留的小数位数
        System.out.println(num2);

方法二

返回一个字符串

        float num = 1.23456f;
        DecimalFormat df = new DecimalFormat("0.00");
        String num3 = df.format(num);
        System.out.println(num3);                    

 

float保留指定位数的小数

标签:

原文地址:http://www.cnblogs.com/yeming/p/5437311.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!