如何进行Java Float保留小数位精度的实现,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
DecimalFormat decimalFormat=new DecimalFormat(".00");
return Float.valueOf(super.getDecimalFormat().format(new BigDecimal(handleTime)));
用过两种方法:DecimalFormat和Math.round()。
用法:
String java.text.NumberFormat.format(double number)方法
float f = 0.5555f;
DecimalFormat df1 = new DecimalFormat("#.00");//保留两位小数,如果是零点几,则小数点前的0不显示,小数点后几个零就保留几位
df1.format(f);
#表示该位如果是0则不必显示出来,0则表示该位如果是0仍然显示;
函数的定义是:;
所以,传参是double,也可以传float(隐式转换),最后的结果是String类型。
int java.lang.Math.round(float a)方法
float f = 1.222f;
f = Math.round(f * 100) / 100f;//乘以100,然后除以100转换为浮点类型
/**乘以多少就保留多少位小数
**注意Math.round()方法传float类型!
*/
两种方法都会四舍五入。
如果是浮点类型建议用Math.round方法,也可以根据自己需求diy代码。
详细讲解请看代码注释和控制台输出:(包含decimalFloat的格式、Math.round函数的实现逻辑)
package testMap;
import java.text.DecimalFormat;
public class TestFloat {
public static void main(String[] args) {
// TODO Auto-generated method stub
//四舍五入保留两位
float f = 0.5555f;
//decimalFormat是将double类型数据转换为字符串,并进行格式化
//#表示这位如果是0则不必显示出来,0则表示这位如果是
//format函数:String java.text.NumberFormat.format(double number)
DecimalFormat df1 = new DecimalFormat("#.00");//首位0不显示出来,即0.1显示为 .1
DecimalFormat df2 = new DecimalFormat("0.00");//首位0显示出来,即0.1显示为 0.1
System.out.println("--------DecimalFormat----------");
System.out.println("df1==" + df1.format(f));
System.out.println("df2==" + df2.format(f));
System.out.println(df1.format(f).getClass());//String类型
System.out.println(Float.parseFloat(df1.format(f)));//转换为float类型
System.out.println(String.valueOf(df1.format(f)));
// System.out.println(Float.toString(df1.format(f)));//转换为String类型
f = 0.595f;
//Math.round()方法是将浮点类型数据 乘以10的多少次方 ,取整,然后 + 0.5 除以10的多少次方,取小数点后多少位
// 如乘以1000 则取小数点后3位
System.out.println("---------Math.round()----------");
System.out.println(Math.round(f * 100) / 100f);//四舍五入后如果末尾是0,自动省略,不显示
// System.out.println(df1.format("1.2"));//参数必须是数值型String java.text.NumberFormat.format(double number)
System.out.println(Float.toString(f));//转换为String输出效果
System.out.println(Float.toString(f));//转换为String输出效果
System.out.println("-----------Math.round()的正数非特殊值实现逻辑--------------");
f = 11.115111f;
int b = (int) (f * 100 + 0.5);
float a = b / 100f;
System.out.println("a==" + a);
System.out.println((int)(f * 100 + 0.5) / 100f);
f = -12.115f;
System.out.println("负数" + Math.round(f * 100) / 100f);
f = -12.116f;
System.out.println("负数" + Math.round(f * 100) / 100f);
System.out.println("-------Math.round()的负数非特殊值实现逻辑--------");
int c = (int) (f * 100 - 0.5);
float d = c / 100f;
System.out.println("d==" + d);
System.out.println((int)(d * 100 - 0.5) / 100f);
}
}
控制台输出:
截图如下:
----下面的是控制台输出-----(和上面一样的,怕图片丢失)
--------DecimalFormat----------
df1==.56
df2==0.56
class java.lang.String
0.56
.56
---------Math.round()----------
0.6
0.595
0.595
-----------Math.round()的正数非特殊值实现逻辑--------------
a==11.12
11.12
负数-12.11
负数-12.12
-------Math.round()的负数非特殊值实现逻辑--------
d==-12.12
-12.12
顺便贴上NumberFormat.formart()的代码:
/**
* Specialization of format.
*
* @param number the double number to format
* @return the formatted String
* @exception ArithmeticException if rounding is needed with rounding
* mode being set to RoundingMode.UNNECESSARY
* @see java.text.Format#format
*/
public final String format(double number) {
// Use fast-path for double result if that works
String result = fastFormat(number);
if (result != null)
return result;
return format(number, new StringBuffer(),
DontCareFieldPosition.INSTANCE).toString();
}
关于如何进行Java Float保留小数位精度的实现问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。