Morteza majideyan
Morteza majideyan

Reputation: 15

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

Upvotes: 0

Views: 957

Answers (2)

Mudit Goel
Mudit Goel

Reputation: 354

Following code will work for coversion in Kotlin :

val floatValue = 12.642054
val parsedValue = ((floatValue * 10).toInt()).toFloat() / 10

Upvotes: 1

shahram_javadi
shahram_javadi

Reputation: 289

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

Upvotes: 4

Related Questions