ePointRight和scaleByPowerOfTen有什么

ePointRight和scaleByPowerOfTen有什么

本文介绍了BigDecimal movePointRight和scaleByPowerOfTen有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

BigDecimal x = new BigDecimal("34.5678");
BigDecimal a = x.movePointRight(3);
BigDecimal b = x.scaleByPowerOfTen(3);
BigDecimal c = x.movePointRight(-3);
BigDecimal d = x.scaleByPowerOfTen(-3);

a和b均为34567.8,c和d均为0.0345678。
a.scale() b.scale 均为1且 c .scale() d.scale()都是7。

a and b are both 34567.8 and c and d are both 0.0345678.a.scale() and b.scale are both 1 and c.scale() and d.scale() are both 7.

In这两种方法在什么情况下产生不同的结果?

In what circumstances do these two methods produce different results?

推荐答案


  • movePointRight 将防止出现负面比例,如果它导致一个。

  • scaleByPowerOfTen 不会阻止此。

    • movePointRight will prevent a negative scale from occurring if it results in one.
    • scaleByPowerOfTen will not prevent this.
    • 示例代码:

      import java.math.BigDecimal;
      
      public class BigDecimalScale {
        public static void main(String... args) {
          long base = 12345;
          int scale = 4;
      
          BigDecimal number = BigDecimal.valueOf(base, scale);
          System.out.println(number);
          BigDecimal pointRight = number.movePointRight(5);
          System.out.println(pointRight + "; my scale is " + pointRight.scale());
          BigDecimal scaleBy = number.scaleByPowerOfTen(5);
          System.out.println(scaleBy + "; my scale is " + scaleBy.scale());
        }
      }
      

      结果:

      1.2345
      123450; my scale is 0
      1.2345E+5; my scale is -1
      

      这篇关于BigDecimal movePointRight和scaleByPowerOfTen有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 02:51