cast Long to BigDecimal

后端 未结 6 2047
一向
一向 2021-01-03 17:34

How can I cast Long to BigDecimal?

相关标签:
6条回答
  • 2021-01-03 18:07

    you have to create a new bigDecimal

    how to do it

    0 讨论(0)
  • 2021-01-03 18:11

    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

    0 讨论(0)
  • 2021-01-03 18:14

    You need to create a new BigDecimal object

      Long test = new Long (10);
      BigDecimal bigD = new BigDecimal(test.longValue());
    
    0 讨论(0)
  • 2021-01-03 18:15

    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);
    
    0 讨论(0)
  • 2021-01-03 18:30

    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.

    0 讨论(0)
  • 2021-01-03 18:33

    You'll have to create a new BigDecimal.

    BigDecimal d = new BigDecimal(long);
    
    0 讨论(0)
提交回复
热议问题