نکات کاربردی

طبقه بندی موضوعی

آخرین مطالب

۲ مطلب با کلمه‌ی کلیدی «java» ثبت شده است

It is not a good approach to use double for precise values, such as currency.

BigDecimal for the Rescue

BigDecimal represents a signed decimal number of arbitrary precision with an associated scale. BigDecimal provides full control over the precision and rounding of the number value. Virtually, it's possible to calculate the value of pi to 2 billion decimal places using BigDecimal, with available physical memory being the only limit.

That’s the reason why we should always prefer BigDecimal or BigInteger for financial calculations.

 

Special Notes

Primitive type: int and long are also useful for monetary calculations if the decimal precision is not required.

We should really avoid using the BigDecimal (double value) constructor, and instead, we should really prefer using the BigDecimal(String), because BigDecimal (0.1) results in (0.1000000000000000055511151231257827021181583404541015625) being stored in the BigDecimal instance. In contrast, BigDecimal ("0.1") stores exactly 0.1.


What Every Computer Scientist Should Know About Floating-Point Arithmetic

behrad nasehi
۱۴ تیر ۰۰ ، ۱۴:۱۰ موافقین ۰ مخالفین ۰ ۰ نظر
behrad nasehi
۲۷ ارديبهشت ۰۰ ، ۱۳:۱۳ موافقین ۰ مخالفین ۰ ۰ نظر