Retain precision with double in Java

前端 未结 22 3133
情歌与酒
情歌与酒 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:57

    When you input a double number, for example, 33.33333333333333, the value you get is actually the closest representable double-precision value, which is exactly:

    33.3333333333333285963817615993320941925048828125
    

    Dividing that by 100 gives:

    0.333333333333333285963817615993320941925048828125
    

    which also isn't representable as a double-precision number, so again it is rounded to the nearest representable value, which is exactly:

    0.3333333333333332593184650249895639717578887939453125
    

    When you print this value out, it gets rounded yet again to 17 decimal digits, giving:

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

    Multiply everything by 100 and store it in a long as cents.

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

    Do not waste your efford using BigDecimal. In 99.99999% cases you don't need it. java double type is of cource approximate but in almost all cases, it is sufficiently precise. Mind that your have an error at 14th significant digit. This is really negligible!

    To get nice output use:

    System.out.printf("%.2f\n", total);
    
    0 讨论(0)
  • 2020-11-21 05:59

    As others have noted, not all decimal values can be represented as binary since decimal is based on powers of 10 and binary is based on powers of two.

    If precision matters, use BigDecimal, but if you just want friendly output:

    System.out.printf("%.2f\n", total);
    

    Will give you:

    11.40
    
    0 讨论(0)
  • 2020-11-21 06:00

    If you have no choice other than using double values, can use the below code.

    public static double sumDouble(double value1, double value2) {
        double sum = 0.0;
        String value1Str = Double.toString(value1);
        int decimalIndex = value1Str.indexOf(".");
        int value1Precision = 0;
        if (decimalIndex != -1) {
            value1Precision = (value1Str.length() - 1) - decimalIndex;
        }
    
        String value2Str = Double.toString(value2);
        decimalIndex = value2Str.indexOf(".");
        int value2Precision = 0;
        if (decimalIndex != -1) {
            value2Precision = (value2Str.length() - 1) - decimalIndex;
        }
    
        int maxPrecision = value1Precision > value2Precision ? value1Precision : value2Precision;
        sum = value1 + value2;
        String s = String.format("%." + maxPrecision + "f", sum);
        sum = Double.parseDouble(s);
        return sum;
    }
    
    0 讨论(0)
  • 2020-11-21 06:01

    If you just want to process values as fractions, you can create a Fraction class which holds a numerator and denominator field.

    Write methods for add, subtract, multiply and divide as well as a toDouble method. This way you can avoid floats during calculations.

    EDIT: Quick implementation,

    public class Fraction {
    
    private int numerator;
    private int denominator;
    
    public Fraction(int n, int d){
        numerator = n;
        denominator = d;
    }
    
    public double toDouble(){
        return ((double)numerator)/((double)denominator);
    }
    
    
    public static Fraction add(Fraction a, Fraction b){
        if(a.denominator != b.denominator){
            double aTop = b.denominator * a.numerator;
            double bTop = a.denominator * b.numerator;
            return new Fraction(aTop + bTop, a.denominator * b.denominator);
        }
        else{
            return new Fraction(a.numerator + b.numerator, a.denominator);
        }
    }
    
    public static Fraction divide(Fraction a, Fraction b){
        return new Fraction(a.numerator * b.denominator, a.denominator * b.numerator);
    }
    
    public static Fraction multiply(Fraction a, Fraction b){
        return new Fraction(a.numerator * b.numerator, a.denominator * b.denominator);
    }
    
    public static Fraction subtract(Fraction a, Fraction b){
        if(a.denominator != b.denominator){
            double aTop = b.denominator * a.numerator;
            double bTop = a.denominator * b.numerator;
            return new Fraction(aTop-bTop, a.denominator*b.denominator);
        }
        else{
            return new Fraction(a.numerator - b.numerator, a.denominator);
        }
    }
    
    }
    
    0 讨论(0)
提交回复
热议问题