如何在 Java 中将分数格式的字符串转换为小数或浮点数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13249858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert from fraction formatted string to Decimal or Float in Java?
提问by TechnoGeezer
I have few string values which I am fetching from my database e.g
我从数据库中获取的字符串值很少,例如
"1/4","2/3"
But while displaying as Android ListView contents I need to display it as 0.25
,0.66
.
但是在显示为 Android ListView 内容时,我需要将其显示为0.25
, 0.66
。
Now I don't want to split the string and then covert to individual strings to numbers and then divide them to have result.
现在我不想拆分字符串,然后将单个字符串转换为数字,然后将它们分开以获得结果。
Does anyone know, any direct functions like Double.valueOf
or parseDouble
kind?
有谁知道,任何直接的功能喜欢Double.valueOf
或parseDouble
种类?
回答by mishadoff
Why you "dont want to split the string and then covert to individual strings to numbers and then divide them to have result"?
为什么您“不想拆分字符串,然后将单个字符串转换为数字,然后将它们分开以获得结果”?
I am not aware of any built-in function to do that so the simplest solution:
我不知道有任何内置函数可以做到这一点,所以最简单的解决方案是:
double parse(String ratio) {
if (ratio.contains("/")) {
String[] rat = ratio.split("/");
return Double.parseDouble(rat[0]) / Double.parseDouble(rat[1]);
} else {
return Double.parseDouble(ratio);
}
}
It also covers the case where you have integer representation of ratio
它还涵盖了比例的整数表示的情况
parse("1/2") => 0.5
parse("3/7") => 0.42857142857142855
parse("1") => 1.0
回答by Cameron Stearns
I imagine you have solved this problem in the last 2 years; however, you can use the Apache commons fraction class.
我想你在过去的两年里已经解决了这个问题;但是,您可以使用Apache 公共分数类。
It has a built in parser, so if you were to call:
它有一个内置的解析器,所以如果你要调用:
Fraction fraction = Fraction.getFraction("1/2");
double d = fraction.doubleValue();
Then d should contain .5.
那么 d 应该包含 0.5。
回答by jmrodrigg
You can split the fraction using split("/")
. Then you can convert the values to Double
and perform the division. I have no idea of Android, that's how I'd do it in Java.
您可以使用 分割分数split("/")
。然后您可以将值转换为Double
并执行除法。我对 Android 一无所知,这就是我在 Java 中的做法。
回答by Antrromet
回答by gabga
I don't think that there is anything like that. But I don't see why you wouldn't create your own function like this:
我不认为有这样的事情。但我不明白为什么你不会像这样创建自己的函数:
public static double fromStringFraction(String fraction){
String[] fractionArray = fraction.split("/");
try {
if (fractionArray.length != 2){
if (fractionArray.length == 1){
return Double.parseDouble(fractionArray[0]);
}
else {
return 0d;
}
}
double b = Double.parseDouble(fractionArray[1]);
if (b==0d){
return 0d;
}
Double a = Double.parseDouble(fractionArray[0]);
return a/b;
}
catch (NumberFormatException e){
return 0d;
}
}
回答by Devangi Desai
try using
尝试使用
SpannableStringBuilder test = new SpannableStringBuilder();
test.append("\n");
test.append(Html.fromHtml("<sup>5</sup>/<sub>9</sub>"));
test.append("\n");