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
bigdecimal除法保留小数* 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;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论