نکات کاربردی

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

آخرین مطالب

What Every Computer Scientist Should Know About Floating-Point Arithmetic

دوشنبه, ۱۴ تیر ۱۴۰۰، ۰۲:۱۰ ب.ظ

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

double

float

java

نظرات  (۰)

هیچ نظری هنوز ثبت نشده است

ارسال نظر

ارسال نظر آزاد است، اما اگر قبلا در بیان ثبت نام کرده اید می توانید ابتدا وارد شوید.
شما میتوانید از این تگهای html استفاده کنید:
<b> یا <strong>، <em> یا <i>، <u>، <strike> یا <s>، <sup>، <sub>، <blockquote>، <code>، <pre>، <hr>، <br>، <p>، <a href="" title="">، <span style="">، <div align="">
تجدید کد امنیتی