将字符串(例如“ 5:4”)转换为数值的最简单方法是什么

时间:2019-05-04 02:12:17

标签: android kotlin

我正在使用具有上述格式的字符串,例如“ 5:4”,我需要获取它们的值(例如“ 5:4”等于1.25)。

一个简单的.toDouble(),什么是最好的方法

3 个答案:

答案 0 :(得分:1)

您可以<!-- FILE: index.html--> <!DOCTYPE html> <html> <head> <title>Markdown TexDollar Reader</title> <!-- Javascript setup using node.js: -> <!-- C:\demo> npm install mathjax --> <!-- C:\demo> npm install showdown --> <script type="text/javascript" src="./node_modules/showdown/dist/showdown.js"></script> <script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: {inlineMath: [["$","$"],["\\(","\\)"]]} }); </script> <script type="text/javascript" src="./node_modules/mathjax/MathJax.js?config=TeX-AMS_HTML-full"></script> </head> <body> <script> converter = new showdown.Converter(); <!-- FIXME: Instead of setting text string manually from javascript, i want to load my file in http directory called "markdown.md" into the javascript string text--> text = '# hello, markdown!'; text += '\nthis is a test'; text += '\n$$e=mc^2$$'; html = converter.makeHtml(text); document.write(html); </script> </body> </html>

Iterable.reduce()

答案 1 :(得分:0)

使用String.split()方法的普通Kotlin方法看起来像这样:

// INPUT
val ratio = "5:4" // can be "5/4" as well

// splits given string into a list and then cast it's members into Double
val items : List<Double> = (ratio.split(":", "/")).map { it.toDouble() } // [5, 4]

var result = 0.0
// calculate the ratio
if (items.size == 2 && items[1] != 0.0)
    result = items[0] / items[1]

println(result) // 1.25

答案 2 :(得分:-2)

String a="5:4";
a= a.replaceAll(":", "/");
float result = new Expression(a).eval();