Retain precision with double in Java

前端 未结 22 3132
情歌与酒
情歌与酒 2020-11-21 05:15
public class doublePrecision {
    public static void main(String[] args) {

        double total = 0;
        total += 5.6;
        total += 5.8;
        System.out         


        
相关标签:
22条回答
  • 2020-11-21 05:48

    Use java.math.BigDecimal

    Doubles are binary fractions internally, so they sometimes cannot represent decimal fractions to the exact decimal.

    0 讨论(0)
  • 2020-11-21 05:52

    Why not use the round() method from Math class?

    // The number of 0s determines how many digits you want after the floating point
    // (here one digit)
    total = (double)Math.round(total * 10) / 10;
    System.out.println(total); // prints 11.4
    
    0 讨论(0)
  • 2020-11-21 05:53
    private void getRound() {
        // this is very simple and interesting 
        double a = 5, b = 3, c;
        c = a / b;
        System.out.println(" round  val is " + c);
    
        //  round  val is  :  1.6666666666666667
        // if you want to only two precision point with double we 
                //  can use formate option in String 
               // which takes 2 parameters one is formte specifier which 
               // shows dicimal places another double value 
        String s = String.format("%.2f", c);
        double val = Double.parseDouble(s);
        System.out.println(" val is :" + val);
        // now out put will be : val is :1.67
    }
    
    0 讨论(0)
  • 2020-11-21 05:53

    Use a BigDecimal. It even lets you specify rounding rules (like ROUND_HALF_EVEN, which will minimize statistical error by rounding to the even neighbor if both are the same distance; i.e. both 1.5 and 2.5 round to 2).

    0 讨论(0)
  • 2020-11-21 05:54

    You may want to look into using java's java.math.BigDecimal class if you really need precision math. Here is a good article from Oracle/Sun on the case for BigDecimal. While you can never represent 1/3 as someone mentioned, you can have the power to decide exactly how precise you want the result to be. setScale() is your friend.. :)

    Ok, because I have way too much time on my hands at the moment here is a code example that relates to your question:

    import java.math.BigDecimal;
    /**
     * Created by a wonderful programmer known as:
     * Vincent Stoessel
     * xaymaca@gmail.com
     * on Mar 17, 2010 at  11:05:16 PM
     */
    public class BigUp {
    
        public static void main(String[] args) {
            BigDecimal first, second, result ;
            first = new BigDecimal("33.33333333333333")  ;
            second = new BigDecimal("100") ;
            result = first.divide(second);
            System.out.println("result is " + result);
           //will print : result is 0.3333333333333333
    
    
        }
    }
    

    and to plug my new favorite language, Groovy, here is a neater example of the same thing:

    import java.math.BigDecimal
    
    def  first =   new BigDecimal("33.33333333333333")
    def second = new BigDecimal("100")
    
    
    println "result is " + first/second   // will print: result is 0.33333333333333
    
    0 讨论(0)
  • 2020-11-21 05:54

    Pretty sure you could've made that into a three line example. :)

    If you want exact precision, use BigDecimal. Otherwise, you can use ints multiplied by 10 ^ whatever precision you want.

    0 讨论(0)
提交回复
热议问题