Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

while testing the app that I'm working on right now I stepped into weird problem, my app works fine on emulator but crashes on my device(huawei p9, I don't know if it really matters). It seems like this two particular lines of code are reason of crash(app works fine on device after removing this formatting code):

val formatedBruttoPrice = "%.2f".format(price).toDouble()         // price is a double
val formated            = "%.2f".format(totalPrice).toDouble()    // totalPrice is also a double

The error that I get java.lang.NumberFormatException: For input string: "2,0"

Any ideas why? And how to fix this?

question from:https://stackoverflow.com/questions/66059334/why-format-crashes-an-app-only-on-device

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
250 views
Welcome To Ask or Share your Answers For Others

1 Answer

That is due to differences in locales. The emulator converts 2.0 to "2.0" due to english locales, but your device uses something else which produces "2,0". Then you are calling .toDouble() so it tries to parse. So you went from double to string to double. Just remove the .toDouble() calls and use the strings instead.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...