Round number to only first decimal place

前端 未结 3 573
渐次进展
渐次进展 2021-01-21 10:17

I would like to know the available phone memory, so I wrote this code.

File path2 = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
lo         


        
3条回答
  •  伪装坚强ぢ
    2021-01-21 10:53

    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();
    

提交回复
热议问题