java 两个整数相除 结果为一位小数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 两个整数相除 结果为一位小数相关的知识,希望对你有一定的参考价值。
public static void main(String[] args)
double ans = 3/4;
System.out.println("The answer is " + ans);
输出为0.0
既然两个整数相除的结果默认为整型,为什么这里还会输出.0?
这里的.0不是四舍五入的结果,那么应该怎么解释?
因为ans的类型为double
其实你这个式子就和
double ans=0是等价的,虽然是给的整数,但在储存的时候,已经做了隐式类型转换为double了,输出时会按类型格式输出,double当然要保留小数位,另外,他的小数不是四舍五入得来的,而是空位补0。
3/4明显大于0.5,四舍五入的话应该为1才对 参考技术A 两步操作: 3/4 没有指定浮点数,结果为整型0.
然后再类型转化成了double,如果不指定小数位数会自动给带一位小数。 参考技术B double 是精度问题,和3/4没有关系。
举个例子:
int ans=3/4;
int ans =0.3/0.4;
结果都是0,不会出现0.0。
这个只是精度的问题。 参考技术C double ans = 3.d / 4;
精度问题,以上即可 参考技术D 你这个情况叫取整
java一个整数除以一个小数为啥的到小数
Java中如果除运算符“/”,在不加任何限制的情况下,两个整数相除,得到的是整数,小数点后的被舍弃。但是有些场景下我们需要拿到除得的小数,还要指定位数的小数。这时候有以下处理方法:1.使用DecimalFormat来限定得到的小数位数
int pcm = 98;
int fcm = 11;
DecimalFormat df = new DecimalFormat("0.00");
double tmpVal = Double.parseDouble(df.format((double) pcm/(pcm+fcm)));
//get value 0.89
注意,它默认返回的是String,如果需要double/float要做一下转换。
2.直接使用Decimal运算
@Test
public void testDecimalOper()
int pcm = 94;
int fcm = 11;
BigDecimal pcmbd = new BigDecimal(pcm);
BigDecimal fcmbd = new BigDecimal(fcm);
BigDecimal rate = new BigDecimal(0.00);
rate = pcmbd.divide(pcmbd.add(fcmbd), 2, RoundingMode.HALF_UP);
System.out.println(rate);//0.90
float/double在工程运算中使用的比较多,在商业计算中使用Decimal类型的比较多。(注:
在《Effective Java》这本书中也提到这个原则,float和double只能用来做科学计算或者是工程计算,在商业计算中我们要用 java.math.BigDecimal,另外,我们如果需要精确计算,要用String来够造BigDecimal。在《Effective Java》一书中的例子是用String来够造BigDecimal的。(注意:divide方法中推荐使用枚举RoundingMode.HALF_UP)
)
两种方式都可以。推荐使用第二种方式来处理精度和round mode的设置。
附BigDecimal rouding mode:
/**
* Rounding mode to round away from zero. Always increments the
* digit prior to a nonzero discarded fraction. Note that this rounding
* mode never decreases the magnitude of the calculated value.
*/
public final static int ROUND_UP = 0;
/**
* Rounding mode to round towards zero. Never increments the digit
* prior to a discarded fraction (i.e., truncates). Note that this
* rounding mode never increases the magnitude of the calculated value.
*/
public final static int ROUND_DOWN = 1;
/**
* Rounding mode to round towards positive infinity. If the
* @code BigDecimal is positive, behaves as for
* @code ROUND_UP; if negative, behaves as for
* @code ROUND_DOWN. Note that this rounding mode never
* decreases the calculated value.
*/
public final static int ROUND_CEILING = 2;
/**
* Rounding mode to round towards negative infinity. If the
* @code BigDecimal is positive, behave as for
* @code ROUND_DOWN; if negative, behave as for
* @code ROUND_UP. Note that this rounding mode never
* increases the calculated value.
*/
public final static int ROUND_FLOOR = 3;
/**
* Rounding mode to round towards @literal "nearest neighbor"
* unless both neighbors are equidistant, in which case round up.
* Behaves as for @code ROUND_UP if the discarded fraction is
* ≥ 0.5; otherwise, behaves as for @code ROUND_DOWN. Note
* that this is the rounding mode that most of us were taught in
* grade school.
*/
public final static int ROUND_HALF_UP = 4;
/**
* Rounding mode to round towards @literal "nearest neighbor"
* unless both neighbors are equidistant, in which case round
* down. Behaves as for @code ROUND_UP if the discarded
* fraction is @literal > 0.5; otherwise, behaves as for
* @code ROUND_DOWN.
*/
public final static int ROUND_HALF_DOWN = 5;
/**
* Rounding mode to round towards the @literal "nearest neighbor"
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor. Behaves as for
* @code ROUND_HALF_UP if the digit to the left of the
* discarded fraction is odd; behaves as for
* @code ROUND_HALF_DOWN if it's even. Note that this is the
* rounding mode that minimizes cumulative error when applied
* repeatedly over a sequence of calculations.
*/
public final static int ROUND_HALF_EVEN = 6;
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary. If this rounding mode is
* specified on an operation that yields an inexact result, an
* @code ArithmeticException is thrown.
*/
public final static int ROUND_UNNECESSARY = 7; 参考技术A java一个整数除以一个小数为什么的到小数
主要是因为java中的【简单类型】636f707962616964757a686964616f31333332643230并不适用于对【浮点】的【 精确计算】。,其它语言也存在同样的问题。
以上是关于java 两个整数相除 结果为一位小数的主要内容,如果未能解决你的问题,请参考以下文章