如何在Android Studio中将字符串“1/4”转换为双精度而不引起NumberFormatException?

问题描述 投票:0回答:1

我正在从用户处获取 EditText 输入,例如“1/3”、“1/4”等。此输入必须转换为 Double。

这就是我试图用代码做的事情:

double shareDouble= Double.parseDouble(etShare.getText().toString());

但这会抛出 NumberFormatException。我做错了什么。请帮忙..

android android-studio
1个回答
0
投票

分割字符串

val s = "1/4"
val splitString = s.split("/")
val top = splitString[0].toInt()
val bottom = splitString[1].toInt()
    
val number =  (top * 1.0) / bottom
© www.soinside.com 2019 - 2024. All rights reserved.