Geo*_*mas -2 java double currency
在Java中,将Double值的Dollar转换为cents的美分的最佳方法是什么。目前,我使用以下方法:
Double cents = new Double(dollar*100);
int amount = cents.intValue();
Run Code Online (Sandbox Code Playgroud)
这种方法有多精确?有没有更好的方法可以做到这一点。
由于您已将值加倍,因此您已经引入了一些不精确的含义:您存储的数字可能与您要存储的值不完全相同。为了解决这个问题,我建议将其四舍五入到最接近的一分。您可以使用Math.round:
int cents = (int) Math.round(100*dollars);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2979 次 |
| 最近记录: |