zoukankan      html  css  js  c++  java
  • java 保留小数点后N位数(若干位),几种实现的方式总结

    import java.math.BigDecimal;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;
    /**
    * java 保留小数点后N位数(若干位)位,几种实现的方式总结
    * (1)常用的是1.DecimalFormat,和2.BigDecimal
    * (2)4.String .format("%.2f",dbstr);
    * @author zhangqf
    *
    */
    public class BigDecimalDemo01 {
    public static void main(String[] args) {

    /**1.DecimalFormat**/
    DecimalFormat decimalFormat=new DecimalFormat();
    decimalFormat.applyPattern("############.###############");
    //decimalFormat.applyPattern("#.00");//decimalFormat.applyPattern("###,###,###,###.###");
    // #.00 表示两位小数 #.0000四位小数 以此类推.
    String df="0.000000520";
    double d=Double.parseDouble(df);
    String testdf=decimalFormat.format(d);
    //切记:decimalFormat.format(d);格式里面放的是d是double类型的,不可把df字符串String放进去,
    //写代码时不会报错,但运行的时候会报:不合法参数异常
    System.out.println("testdf:="+testdf);
    /**1.1.DecimalFormat变形使用,当输入数较大16位左右或小数位0超过5位以上例如:0.00000005,就会变成科学计数法的形式**/
    String strdf1="12.0123";
    String strdf2="0.000000005";
    String strMul=mul(strdf1, strdf2);
    System.out.println("strMul:="+strMul);
    /*1.和1.1DecimalFormat运行结果
    * 0.00000052----testdf
    61.7283945617283945====strMul*/

    /**2.BigDecimal,一般也用于java计算器加减乘除**/
    double bdf=0.0078123456789;
    BigDecimal bigDecimal=new BigDecimal(bdf);
    double bdfScale=bigDecimal.setScale(10, BigDecimal.ROUND_HALF_UP).doubleValue();
    /*double bdfScale=bigDecimal.setScale(10, BigDecimal.ROUND_HALF_UP).doubleValue();//四舍五入,保留2位小数
    double bdf=0.000000078;--小数点后面的0不能多于5个,否则就是科学计数法-->运行结果:7.8E-8=====bdfScale
    但是,0.0078123456789这种就不会变成科学计数法
    */
    System.out.println("bdfScale:="+bdfScale);

    /**3.NumberFormat,数字格式化---数字,货币,百分数,后续专门详细写这个模块,
    * 创建格式化器(默认地区Local格式):
    NumberFormat.getNumberInstance();
    NumberFormat.getCurrencyInstance();
    NumberFormat.getPercentInstance();
    * **/
    Double dbNumberF=new Double("123456789.1236");
    System.out.println("dbNumberF:="+dbNumberF);
    NumberFormat numberFormat=java.text.NumberFormat.getInstance();
    numberFormat.setGroupingUsed(false);//只保留了3位,也会四舍五入
    String dbNumFstr=numberFormat.format(dbNumberF);
    System.out.println("dbNumberF:"+dbNumFstr);
    /* Double dbNumberF=new Double("123456789.123456789");
    dbNumberF:=1.2345678912345679E8
    dbNumberF:123456789.123*/
    /**运行结果:
    * dbNumberF:=1.234567891236E8
    dbNumberF:123456789.124**/

    /*3.1变式2
    用NumberFormat的setMaximumFractionDigits方法实现
    NumberFormat format=NumberFormat.getNumberInstance() ;
    format.setMaximumFractionDigits(int digits) //digits为 显示的数字位数
    */

    double number = 23.5455;
    NumberFormat format = NumberFormat.getNumberInstance() ;
    format.setMaximumFractionDigits(2);
    String result = format.format(number) ;
    System.out.println("result:="+result);//23.55


    /**4.String .format(),一般也用于java计算器加减乘除**/
    double dbstr=3.14625789;
    String resultStr = String .format("%.2f",dbstr);
    /*这个小数格式化:位数不够用0来补,也可四舍五入
    String resultStr = String .format("%.2f",dbstr);
    3.1462578900*/

    System.out.println("resultStr:="+resultStr);


    /**5.利用算法实现**/
    double numberArith=0.562;//可以是int,也可以是double
    /*对于这种类型是有精度丢失,不成立的;double numberArith=0.000000006;输出结果:0.0*/
    double resultArith = ((int) (numberArith * 100)) / 100.0;
    System.out.println("resultArith:="+resultArith);
    }

    /**
    * * 两个Double数相乘 *
    *
    * @param v1
    * *
    * @param v2
    * *
    * @return String 
    */
    public static String mul(String v1, String v2) {
    BigDecimal b1 = new BigDecimal(v1);
    BigDecimal b2 = new BigDecimal(v2);
    return numFormat(b1.multiply(b2).toString());
    }

    public static String numFormat(String resultFormat){//double d = 41.123;
    String d = resultFormat;
    if (d.contains(".")) {
    String dot=d.substring(d.indexOf("."), d.length());
    DecimalFormat df = new DecimalFormat("#.####################");
    //不用指定整数位多少位,这样比较好,以免数很大造成科学计数法
    double db=Double.valueOf(dot);
    String str=df.format(db);
    str=d.substring(0, d.indexOf("."))+str.substring(1, str.length());
    //System.out.println(str);
    return str;
    }
    return d;
    }
    }

    运行输出结果:

    testdf:=0.00000052
    strMul:=6.0000000000615
    bdfScale:=0.0078123457
    dbNumberF:=1.234567891236E8
    dbNumberF:123456789.124
    result:=23.55
    resultStr:=3.15
    resultArith:=0.56

  • 相关阅读:
    关于JEE web项目 Servlet中 “/” 的解释 ;
    通过session 怎么防止表单的重复提交!
    Struts hibernate Spring 框架原理
    apache DBUtils 使用例子demo
    二十三种设计模式通俗理解
    怎样将一个脚本添加到开机自启动中
    zabbix
    数据库
    nginx服务器
    Luogu P3398 仓鼠找sugar
  • 原文地址:https://www.cnblogs.com/zhangqf/p/6179945.html
Copyright © 2011-2022 走看看