Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - How to convert Double to String in Kotlin without scientific notation?

I want to set in text view full value of Double without scientific notation (Exponential), what is best approach?

I tried convert Double value using method toString() on it, but I still have scientific value.

tvDepth.text=depth.toString()

I entered value 123456789.123456789 and expect same value in textView, but instead I have: 1.23456789123456789E8.

I don't know how many decimal places I need, so I couldn't format it before set it in text view. I read that in Java is available static method

Double.toString(Double d)

but it's not available in Kotlin. Is there any good approach to show this value correctly?

like image 450
Damian JK Avatar asked Sep 10 '25 19:09

Damian JK


1 Answers

You can use .toBigDecimal().toPlainString() on your Double. For example:

println(PI.toBigDecimal().toPlainString())

prints out:

3.141592653589793
like image 72
Sandi Avatar answered Sep 12 '25 09:09

Sandi