0

I am trying rounding and format techniques in Java as I have to keep my values in database with some specific formats.

My database column has the data precision of 18,4 which means 14 integers and 4 decimal places at max.

Now I am trying max possible value case which is 99999999999999.9999. When I execute the below code, I am getting a rounded value of 1000000000000000. However, I want to store the exact value provided.

Can anyone suggest to keep it as it is in java variable?

    DecimalFormat df2 = new DecimalFormat( "#.####" );
    df2.setRoundingMode(RoundingMode.CEILING);
    double number = Double.parseDouble("99999999999999.9999");
    System.out.println("Format: " + df2.format(number));
Jawad-Dev
  • 274
  • 10
  • 31

1 Answers1

1

You can use BigDecimal

BigDecimal number = new BigDecimal("99999999999999.9999");
Nonika
  • 2,490
  • 13
  • 15