zoukankan      html  css  js  c++  java
  • Java float保留两位小数或多位小数

     Java float保留两位小数或多位小数

    方法1:用Math.round计算,这里返回的数字格式的.
        
    float price=89.89;
    int itemNum=3;
    float totalPrice=price*itemNum;
    float num=(float)(Math.round(totalPrice*100)/100);   //如果要求精确4位就*10000然后/10000

    方法2(OK): 用DecimalFormat 返回的是String格式的.该类对十进制进行全面的封装.像%号,千分位,小数精度.科学计算.

    import java.text.DecimalFormat;
    float price=1.2;
    DecimalFormat decimalFormat=new DecimalFormat("0.00");   //构造方法的字符格式这里如果小数不足2位,会以0补足.
    String p=decimalFormat.format(price);   //format 返回的是字符串

  • 相关阅读:
    css概述五
    css概述四
    css概述三
    css概述二
    css概述
    Python的第三方web开发框架Django
    Python中的模块和包
    SQL语句优化
    数据库向Excel写入数据
    动态拼接sql语句
  • 原文地址:https://www.cnblogs.com/emanlee/p/4629477.html
Copyright © 2011-2022 走看看