0

I am making an app which need to display 3 decimal points i achieve this by NumberDecimal but the issue is if 4th digit is >5 then 3rd digit automatically increase. I want to show exact figure for ex :

Result           : 100.2356 
Output           : 100.236  // Applying NumberDecimal

Need this output : 100.235 // I need this output

This is my function :
public String setdecimal(float a) 
{
   NumberFormat formatter = new DecimalFormat("##.###");
   return formatter.format(a);

}
Glenn
  • 12,741
  • 6
  • 47
  • 48
Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39

2 Answers2

2

Use RoundingMode.FLOOR:

NumberFormat formatter = new DecimalFormat("##.###");
formatter.setRoundingMode(RoundingMode.FLOOR);

Depending on how you want to handle negative numbers, you could also use RoundingMode.DOWN, which rounds towards zero.

samgak
  • 23,944
  • 4
  • 60
  • 82
0

You can use below method to truncate the digits after the points without round up the value.

public static String getThreeDigitDisplayValueFromFloat(float value) {

    String price = new DecimalFormat("##.###").format(value);
    return String.format(Locale.getDefault(), "%.3f", Float.parseFloat(price));
}
Chirag Prajapati
  • 337
  • 6
  • 14