Print floats with certain amount of decimal numbers

Hello,

I am trying to print a float with two decimal symbols. How to do that?

2 Likes

Use DecimalFormat

"%.2f".format(value)

6 Likes

is that returning “19.00” for values with 0 as the decimal part?

1 Like

Yes.

1 Like

I know that I am replying on a really old thread, but I found this googling for “format float with point kotlin”.

Note that kotlin.String.format is locale-dependant! Depending on the runtime locale, it will output 11,34 or 11.34. To be honest, I don’t know why the method without locale is not deprecated, because other methods have been deprecated before due to this inconsistent behavior :smiley:

To avoid this, use something like "%2.f".format(Locale.ROOT, value).

I was trying to format Latitude/Longitude and having inconsistent “,”/“.” introduced a bug for us.