在Java中使用Double保持精度public class doublePrecision {
public static void main(String[] args) {
double total = 0;
total += 5.6;
total += 5.8;
System.out.println(total);
}}以上代码打印:11.399999999999我如何才能把它打印出来(或者可以用作)11.4?
3 回答
守着一只汪
TA贡献1872条经验 获得超3个赞
33.33333333333333
33.3333333333333285963817615993320941925048828125
0.333333333333333285963817615993320941925048828125
0.3333333333333332593184650249895639717578887939453125
0.33333333333333326
拉风的咖菲猫
TA贡献1995条经验 获得超2个赞
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/150
提交
取消