Search code examples
javaandroidandroid-studiokotlin

how do i restric a float value to only one placess after decimal in kotlin android


I have a number with some number decimal places, How can i round this float number with one number decimal places

for example 1.366565646 convert to 1.3


Solution

  • In your case I think you need to trim the number not round it, You can use this:

     double d = 1.366565646;
     DecimalFormat df = new DecimalFormat("#.#");
     double p = Double.parseDouble(df.format(d));