package com.operator;
public class Operator {
public static void main(String[] args){
double num=7.6%2.9;
System.out.println(num);
}
}
运行结果是:1.7999999999999998
怎么能使结果变为1.8,double和float怎么能保留自己想要的小数点后几位
import java.math.BigDecimal;
/**
* Created by Chen-D.W on 2017/5/17.
*/
public class Oper {
public static void main(String[] args){
double num=7.6%2.9;
BigDecimal b =new BigDecimal(num);
double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
System.out.println(f1);
}
}
import java.math.BigDecimal; /** * Created by Chen-D.W on 2017/5/17. */ public class Oper { public static void main(String[] args){ double num=7.6%2.9; BigDecimal b =new BigDecimal(num); double f1 = b.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue(); System.out.println(f1); } }
int num = (int)(7.6%2.9);