Skip to content Skip to sidebar Skip to footer

Round Number To Only First Decimal Place

I would like to know the available phone memory, so I wrote this code. File path2 = Environment.getDataDirectory(); StatFs stat2 = new StatFs(path.getPath()); long blockSize2 = sta

Solution 1:

If I understood your question right you need NumberFormat here:

NumberFormatnf= NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.format(5.654707363);

produces 5,7

Solution 2:

If you need the value for display purpose, use NumberFormat (see answer by Adam Arnold).

For numeric rounding (producing the rounded number), use BigDecimal:

double num = 3.141592653589793d;
double rounded = BigDecimal.valueOf(num).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();

Solution 3:

Have you tried using free = Math.round(free * 10) / 10d; instead of free = Math.round(size * 10) / 10d;? I don't see size as a defined variable.

A simple test of

doublefree=5.654707363;
free= Math.round(free*10) /10d;
System.out.println(free);

outputs 5.7.

Post a Comment for "Round Number To Only First Decimal Place"