zoukankan      html  css  js  c++  java
  • 控制数据的小数位数 java / js

    //java一般控制格式都是通过 DecimalFormat 来控制的.下边是个例子.
    
    import java.text.DecimalFormat;
    public class ControlBit {
     public static void main(String[] argu){
     double money = 399.74099999999993;
     DecimalFormat df=new DecimalFormat("#.00");
     System.out.println(df.format(money));
     
     }
    }
    //js中自带方法控制小数点的显示位数
    
    < script language = " JScript " > Number.prototype.toFixed = function(num) {
       
    // 重新构造toFixed方法,IE5.0+ 
      with(Math) this .NO = round( this .valueOf() * pow( 10 ,num)) / pow( 10 ,num);
      return  String( / /. / g.exec( this .NO) ? this .NO: this .NO + " . " + String(Math.pow( 10 ,num)).substr( 1 ,num));

    }
    alert(( 12.9299 ).toFixed( 2 ));
    alert(( 12.9999 ).toFixed( 2 )); 

    </ script >   

    --------------------- 本文来自 生活真美好 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/wyzsc/article/details/6253255?utm_source=copy
    4种方法,都是四舍五入,例:
    
        import java.math.BigDecimal;
        import java.text.DecimalFormat;
        import java.text.NumberFormat;
        public class format {
            double f = 111231.5585;
            public void m1() {
                BigDecimal bg = new BigDecimal(f);
                double f1 = bg.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                System.out.println(f1);
            }
            /**
             * DecimalFormat转换最简便
             */
            public void m2() {
                DecimalFormat df = new DecimalFormat("#.00");
                System.out.println(df.format(f));
            }
            /**
             * String.format打印最简便
             */
            public void m3() {
                System.out.println(String.format("%.2f", f));
            }
            public void m4() {
                NumberFormat nf = NumberFormat.getNumberInstance();
                nf.setMaximumFractionDigits(2);
                System.out.println(nf.format(f));
            }
            public static void main(String[] args) {
                format f = new format();
                f.m1();
                f.m2();
                f.m3();
                f.m4();
            }
        }
    //还有一种直接向上取整数
    <H2 class="title content-title">//java:Java的取整函数</H2>    <DIV id=content class="content mod-cs-content text-content clearfix"> //Math.floor()、Math.ceil()、BigDecimal都是Java中的取整函数,但返回值却不一样
                
                Math.floor()
                通过该函数计算后的返回值是舍去小数点后的数值
                如:Math.floor(3.2)返回3
                Math.floor(3.9)返回3
                Math.floor(3.0)返回3
                
                Math.ceil()
                ceil函数只要小数点非0,将返回整数部分+1
                如:Math.ceil(3.2)返回4
                Math.ceil(3.9)返回4
                Math.ceil(3.0)返回3 </DIV>
  • 相关阅读:
    Curling 2.0
    POJ
    免费馅饼
    每日一九度之 题目1090:路径打印
    每日一九度之 题目1089:数字反转
    每日一九度之 题目1084:整数拆分
    每日一九度之 题目1083:特殊乘法
    每日一九度之 题目1081:递推数列
    每日一九度之 题目1079:手机键盘
    每日一九度之 题目1077:最大序列和
  • 原文地址:https://www.cnblogs.com/yxdmoodoo/p/9722933.html
Copyright © 2011-2022 走看看