Domanda

how to limit the number of digits of the whole number itself not just the fractional part of the number after the decimal?

for example using the number 54321.12574857f with the method DecimalFormat("##0.##"); it should return 543.12 or 321.12, the two decimal places after the decimal are correct however I want to reduce the numbers before the decimal to only 3. how is this done?

after entering the number 54321.12574857 I was using the parameter for the DecimalFormat method of ##0.## and I was expecting 543.12, or 321.12 not what I am getting which is 54321.12

the only numbers changed by DecimalFormat method are those after the decimal

            float inputNumber = 54321.12574857f;

    DecimalFormat decimalFormat = new DecimalFormat("##0.##");
    String newNumber = String.valueOf(decimalFormat.format(inputNumber));

    System.out.println(newNumber);
È stato utile?

Soluzione

Try some thing like this,

    DecimalFormat decimalFormat = new DecimalFormat("##0.##");
    String newNumber = String.valueOf(decimalFormat.format(inputNumber/100));
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top