gpt4 book ai didi

java - 使用 Kotlin 时出现“java.lang.NumberFormatException : Invalid double' for European languages,”

转载 作者:行者123 更新时间:2023-12-02 09:35:00 24 4
gpt4 key购买 nike

我为此找到的每个解决方案都使用 java 而不是 Kotlin,有人可以帮忙吗?我写了一个应用程序,我只是想翻译到一些欧洲国家,但是数学部分正在失败,因为德国人、法国人等使用逗号而不是句号。我不知道如何使用此处的解决方案来解决此问题。

.kt 文件的一部分:

import java.math.RoundingMode
import java.text.DecimalFormat
...
val df = DecimalFormat("#.##")
df.roundingMode = RoundingMode.CEILING
val lengthCalc = 0.01658
val length = df.format(lengthCalc)

感谢任何帮助

编辑:我看过这里:How to change the decimal separator of DecimalFormat from comma to dot/point?但正如我最初所说,所有解决方案都是针对 Java,而不是 Kotlin

最佳答案

我现在无法尝试,但类似

val otherSymbols = DecimalFormatSymbols()
otherSymbols.setDecimalSeparator(',')
otherSymbols.setGroupingSeparator('.')
DecimalFormat df = DecimalFormat("#.##", otherSymbols)
df.roundingMode = RoundingMode.CEILING
val lengthCalc = 0.01658
val length = df.format(lengthCalc)

应该可以解决问题。对于欧洲,您需要使用小数格式符号,因此使用逗号表示小数,使用句号表示千位。

基于区域设置执行此操作的另一种方法是使用 NumberFormat 并将其转换为 DecimalFormat

val df = NumberFormat.getNumberInstance(currentLocale) as DecimalFormat
df.applyPattern("#.##")
...
df.format(lengthCalc)

关于java - 使用 Kotlin 时出现“java.lang.NumberFormatException : Invalid double' for European languages,”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57596002/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com