cast Long to BigDecimal

主宰稳场 提交于 2019-12-18 18:37:09

问题


How can I cast Long to BigDecimal?


回答1:


You'll have to create a new BigDecimal.

BigDecimal d = new BigDecimal(long);



回答2:


For completeness you can use:

// valueOf will return cached instances for values zero through to ten
BigDecimal d = BigDecimal.valueOf(yourLong);

0 - 10 is as of the java 6 implementation, not sure about previous JDK's




回答3:


You should not use BigDecimal d = new BigDecimal(long); !!

The implementation in BigDecimal for longs is not precise. For financial applications this is critical!

But the implementation for the String argument is better! So use something like:

new BigDecimal(yourLong.toString());

There was a talk on http://www.parleys.com/ about this.




回答4:


You can't cast it. You can create a new BigDecimal though. You can get a long from a Long using Long.getLongValue() if you have the non-primitave Long.

BigDecimal bigD = new BigDecimal(longVal);



回答5:


You need to create a new BigDecimal object

  Long test = new Long (10);
  BigDecimal bigD = new BigDecimal(test.longValue());



回答6:


you have to create a new bigDecimal

how to do it



来源:https://stackoverflow.com/questions/921621/cast-long-to-bigdecimal

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!