Why BigDecimal.Sclae() not not working for non decimal value

Sathish David  Kumar N
Sathish David  Kumar N used Ask the Experts™
on
Why BigDecimal.Sclae() not not working for non decimal value

      public static void main(String[] args) {
            // TODO Auto-generated method stub
            formatBD(2,new BigDecimal(10));
            formatBD(2,new BigDecimal(10.0567));
            formatBD(2,new BigDecimal(10.05));
            formatBD(2,new BigDecimal(10.000023));
      }
      
      public static void   formatBD(int sca,BigDecimal amount)
      {
            System.out.println(amount.scale() > 0 ? amount.setScale(2, BigDecimal.ROUND_CEILING) : amount);
      }
}

Output :10
10.06
10.06
10.01
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Why BigDecimal.Sclae() not not working for non decimal value
It's doing what you've told it to. The static method prints the amount without scaling if it finds the BigDecimal to have a scale of zero.
Incidentally, the proper way to use BigDecimal is to construct it with a string

formatBD(2, new BigDecimal("10.0"));

Open in new window

will make it have a scale of 2
Top Expert 2016

Commented:
:)
Thanks for your support

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial