Kotlin JS – 字符串到数字的转换?
如何做Kotlin JS应用程序中的字符串到数字转换。 我使用下面的代码,并有一些麻烦,从HTMLInputElement
值转换为双。
fun c2f(self: Any) { console.log("Self object: ${self} ") val celsius = document.getElementById("celcius")?.getAttribute("value") as Double val fahrenheit = celsius * 1.8 + 32 console.log("Fahrenheit value: ${fahrenheit} ") window.alert("Celcius (${celsius}) -> Fahrenheit (${fahrenheit}) ") }
- 此外,我没有看到任何
toDouble()
函数的String
类的情况下,在JVM应用程序的情况。
回答我自己的问题,这将有助于某人。
您可以使用kotlin.js顶级解析函数进行string
< - > Number
转换。
fun parseInt(s: String, radix: Int = 10): Int fun safeParseInt(s : String) : Int? fun safeParseDouble(s : String) : Double?