Search code examples
javanumber-formatting

How to compulsorily add two decimals in result of type decimal?


  • Result have two decimal points compulsorily(ex: 10.56, 34.56).
  • In my case: 10.0 should be 10.00 and 10.5 should be 10.50 and 10.3494 should be 10.35.

Solution

    • This code worked for me!

      public static void main(String[] args) throws Exception {
      String Input="75.74306816000052";
      String Input2 = "26.9";
      BigDecimal Output= new BigDecimal(Input).setScale(2, RoundingMode.HALF_EVEN);
      System.out.println(Output.setScale(2)); 
      BigDecimal Output2= new BigDecimal(Input2).setScale(2, RoundingMode.HALF_EVEN);
      System.out.println(Output2.setScale(2)); }