Conversion of a dot to comma during the String.format() operation in Kotlin

Viewed 25

I made a search in a list of items coming from the server by a few parameters of different types. An extension to convert the field "amount" of type Long to Double and divide the summ to display portion after dot was created

fun Long.amount(): Double {
return this.toDouble().div(100)

Field from the server:

4623

Converted value to place into search array:

46.23

I have came to the point when the search works until I enter the total summ in the item with 0 after the dot

7400 -> 74.0 //btw, the summ of 74.00 is displayed to the user

I figured out that the reason is in a "trim" operation during conversion to type Double and searched for the solution like toFixed() in Js to throw the right value to the search function.

What I found is String operation on Double or Float type value format() and the next Kotlin extension was made

fun Double.digitsAfterDot(): String {
return String.format("%.2f", this)

And at that point I saw in logs converted values with two zeros but with a comma instead of dot separation.

I made a replace() function to finish the task, but the question for what reason this kind of converson is made, is still there?

I saw answers related to locale(), but unfortunately didn't found much about it in Kotlin doc. And also, why not to make [format] operator more obvious, because the dot in "%.2f" format operator makes it thought as separation with the dot.

0 Answers
Related