<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    java

    tiger

    常用鏈接

    統(tǒng)計(jì)

    my Blogs

    最新評(píng)論

    java取四舍五入的方法

    “帶下劃線內(nèi)容無效"

    兩種不同方法的實(shí)現(xiàn):


    1:
    /**

    * 提供小數(shù)位四舍五入處理。

    * @param v 需要四舍五入的數(shù)字

    * @param scale 小數(shù)點(diǎn)后保留幾位

    * @return 四舍五入后的結(jié)果

    */

    public static double round(double v,int scale){
    String temp="#,##0.";
    for (int i=0;i<scale ;i++ )
    {
    temp+="0";
    }
    return Double.valueOf(new java.text.DecimalFormat(temp).format(v));
    }

    2:數(shù)學(xué)方法
    public static double round2(double d, int scale) {
    long temp=1;
    for (int i=scale; i>;0; i--) {
    temp*=10;
    }
    d*=temp;
    long dl=Math.round(d);
    return (double)(dl)/temp;
    }

     

    鑒于網(wǎng)友的的指出,我重新認(rèn)真研究了一下四舍五入,最終給出正確解法如下:

    import java.math.BigDecimal;
    import java.text.DecimalFormat;

    /**
    * 本例通過對(duì)網(wǎng)上幾種取四舍五入的研究,進(jìn)行了一一測(cè)試。最終通過實(shí)驗(yàn)和理論得出round4為唯一正確的算法。
    * 2008/10/13

    * @author jamezhan
    *
    */
    public class RoundTest {

    public static double round1(double v, int scale) {
    if (scale < 0)
    return v;

    String temp = "#####0.";
    for (int i = 0; i < scale; i++) {
    temp += "0";
    }

    return Double.valueOf(new java.text.DecimalFormat(temp).format(v));
    }

    /**
    * 該算法會(huì)出現(xiàn)中間運(yùn)算后結(jié)果超過Double.MAX_VALUE,所以不推薦使用
    * @param d
    * @param scale
    * @return
    * @throws Exception
    */
    public static double round2(double d, int scale) throws Exception {
    if (scale < 0)
    return d;

    long temp = 1;
    for (int i = scale; i > 0; i--) {
    temp *= 10;
    }

    if (Math.abs(d * temp) > Double.MAX_VALUE)
    throw new Exception("data is too big or too small");

    d *= temp;
    long dl = Math.round(d);
    return (double) (dl) / temp;
    }

    public static double round3(double v, int scale) {
    BigDecimal value = new BigDecimal(v);
    float actualTax = value.setScale(scale, BigDecimal.ROUND_HALF_UP).floatValue();
    return actualTax;
    }

    public static double round4(double v,int scale)
    {
    if(scale<0){
    throw new IllegalArgumentException("The scale must be a positive integer or zero");
    }

    BigDecimal b = new BigDecimal(Double.toString(v));
    BigDecimal one = new BigDecimal("1");
    return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    public static void testRound1(double d, int scale) {
    System.out.println("==========================");
    System.out.println("data:"+ d + "; scale:"+scale);
    double a = round1(d, scale);
    System.out.println(a);

    DecimalFormat df = new DecimalFormat();
    System.out.println("formatted:"+df.format(a));
    }

    public static void testRound2(double d, int scale) {
    try {
    System.out.println("==========================");
    System.out.println("data:"+ d + "; scale:"+scale);
    double a = round2(d, scale);
    System.out.println(a);

    DecimalFormat df = new DecimalFormat();
    System.out.println("formatted:"+df.format(a));
    } catch (Exception e) {
    System.err.println( e.getMessage() );
    }
    }

    public static void testRound3(double d, int scale) {
    try {
    System.out.println("==========================");
    System.out.println("data:"+ d + "; scale:"+scale);
    double a = round3(d, scale);
    System.out.println(a);

    DecimalFormat df = new DecimalFormat();
    System.out.println("formatted:"+df.format(a));
    } catch (Exception e) {
    System.err.println( e.getMessage() );
    }
    }

    public static void testRound4(double d, int scale) {
    try {
    System.out.println("==========================");
    System.out.println("data:"+ d + "; scale:"+scale);
    double a = round4(d, scale);
    System.out.println(a);

    DecimalFormat df = new DecimalFormat();
    System.out.println("formatted:"+df.format(a));
    } catch (Exception e) {
    System.err.println( e.getMessage() );
    }
    }


    public static void main(String[] args) throws Exception {
    System.out.println("****************************** Test round1 ******************************");

    testRound1(Double.MAX_VALUE,2);
    testRound1(1.264,2);
    testRound1(-1.264,2);
    testRound1(1.265,2);//wrong result
    testRound1(-1.265,2);//wrong result
    testRound1(1.266,2);
    testRound1(-1.266,2);
    testRound1(10224948.265,2);//wrong result
    testRound1(-10224948.265,2);//wrong result
    testRound1(-Double.MAX_VALUE, 2);

    System.out.println("****************************** Test round2 ******************************");

    testRound2(Double.MAX_VALUE,2);
    testRound2(1.264,2);
    testRound2(-1.264,2);
    testRound2(1.265,2);//wrong result (java表示小數(shù)0.1的問題導(dǎo)致的 1.265表示為1.2599999904632568)
    testRound2(-1.265,2);//wrong result (由于round算法是先加0.5再運(yùn)算,所以d為負(fù)數(shù)時(shí)且最后一位小數(shù)為5時(shí)結(jié)果是不正確的)
    testRound2(1.266,2);
    testRound2(-1.266,2);
    testRound2(10224948.265,2);
    testRound2(-10224948.265,2);//wrong result
    testRound2(-Double.MAX_VALUE, 2);


    System.out.println("****************************** Test round3 ******************************");

    testRound3(Double.MAX_VALUE,2);//wrong result 
    testRound3(1.264,2);
    testRound3(-1.264,2);
    testRound3(1.265,2);
    testRound3(-1.265,2);
    testRound3(1.266,2);
    testRound3(-1.266,2);
    testRound3(10224948.265,2);//wrong result 
    testRound3(-10224948.265,2);//wrong result
    testRound3(-Double.MAX_VALUE, 2);//wrong result 

    System.out.println("****************************** Test round4 ******************************");

    testRound4(Double.MAX_VALUE,2);
    testRound4(1.264,2);
    testRound4(-1.264,2);
    testRound4(1.265,2);
    testRound4(-1.265,2);
    testRound4(1.266,2);
    testRound4(-1.266,2);
    testRound4(10224948.265,2);
    testRound4(-10224948.265,2);
    testRound4(-Double.MAX_VALUE, 2);

    }
    }


     

    posted on 2006-04-25 11:38 翠竹 閱讀(12862) 評(píng)論(6)  編輯  收藏 所屬分類: java

    評(píng)論

    # re: java取四舍五入的方法 2006-05-30 14:26 rick

    呵呵,和我的大同小異:
    private double round2(double va) {
    // -------->運(yùn)算四舍五入保留小數(shù)點(diǎn)2位
    va=Math.round(va*100.0);
    return va/100;
    }

    private double trunc2(double va){
    // -------->運(yùn)算截?cái)啾A粜?shù)點(diǎn)2位
    va=Math.floor(va*100.0);
    return va/100;
    }  回復(fù)  更多評(píng)論   

    # re: java取四舍五入的方法 2006-06-14 10:05 翠竹

    呵呵,就是!  回復(fù)  更多評(píng)論   

    # re: java取四舍五入的方法 2007-03-24 21:18 我賊帥

    樓主的算法不錯(cuò),你的只適合兩位小數(shù)...  回復(fù)  更多評(píng)論   

    # re: java取四舍五入的方法 2007-04-16 08:52 翠竹

    可以支持多位:@param scale 小數(shù)點(diǎn)后保留幾位   回復(fù)  更多評(píng)論   

    # re: java取四舍五入的方法 2007-09-19 09:21 longxibo

    lz有bug
    round(-10224948.26, 2)出問題
    round2一樣  回復(fù)  更多評(píng)論   

    # re: java取四舍五入的方法 2008-10-13 15:27 翠竹

    @longxibo
    對(duì)了,請(qǐng)你用round4方法。謝謝!!  回復(fù)  更多評(píng)論   

    主站蜘蛛池模板: 久久国产亚洲精品| 国产自国产自愉自愉免费24区| 国产成人免费全部网站| 麻豆一区二区三区蜜桃免费| 亚洲日本va在线视频观看| 99热这里只有精品6免费| 亚洲成av人在线观看网站| 亚洲精品高清国产一线久久| 免费看黄视频网站| 一级毛片成人免费看a| 亚洲自偷自拍另类图片二区| 免费国产不卡午夜福在线| 95老司机免费福利| 麻豆一区二区三区蜜桃免费| 亚洲成a人片在线观看中文!!! | 亚洲国产精品成人AV在线| 亚洲香蕉成人AV网站在线观看| 91久久青青草原线免费| 特级做a爰片毛片免费看| 久久久无码精品亚洲日韩蜜臀浪潮 | 在线播放免费人成毛片乱码| 伊人久久五月丁香综合中文亚洲 | 亚洲中文无码亚洲人成影院| 最新亚洲成av人免费看| 色妞WWW精品免费视频| 日本免费在线中文字幕| 羞羞漫画在线成人漫画阅读免费| 91嫩草私人成人亚洲影院| 亚洲爽爽一区二区三区| 四虎影院在线免费播放| 免费国产黄网站在线观看可以下载 | 久久久久久毛片免费看| 亚洲人成自拍网站在线观看 | 性色av极品无码专区亚洲 | 色天使亚洲综合一区二区| 久久亚洲精品无码VA大香大香| 亚洲日本中文字幕一区二区三区| 成年男女男精品免费视频网站| 99久久99久久精品免费观看 | 亚洲人成网站18禁止一区| 日韩免费视频一区|