[英]How to turn A string into double
I have this string called strg "12*2/5-3.0^3.23"
i want to turn every int in this string into a double.我有一个名为
strg "12*2/5-3.0^3.23"
字符串,我想将此字符串中的每个 int 转换为 double。 I've tried a lot of this and nothing is working.我已经尝试了很多,但没有任何效果。 i've tried splitting the string everywhere there is a special character and then checking if it is already a double or not by doing something like
if(splt[i].contains(".")){isDouble == true;}
if it is a double i don't touch it if it wasn't a double i did something like splt[i]=splt[i]+".0";
我试过在任何有特殊字符的地方分割字符串,然后通过执行类似
if(splt[i].contains(".")){isDouble == true;}
if if(splt[i].contains(".")){isDouble == true;}
if它是一个双倍我不会碰它如果它不是一个双倍我做了类似splt[i]=splt[i]+".0";
to change it to a double.将其更改为双倍。 I'm fine up to this point but how do i change it back into a string and not a split string.
到目前为止我很好,但我如何将它改回字符串而不是拆分字符串。
final class Playground {
private static List<Double> extract(final String input) {
final List<Double> results = new LinkedList<>();
StringBuilder builder = new StringBuilder(1);
for (final char character : input.toCharArray()) {
if (Character.isDigit(character) || (Character.compare(character, '.') == 0)) {
builder.append(character);
} else {
results.add(Double.valueOf(builder.toString()));
builder = new StringBuilder(1);
}
}
if (builder.length() > 0) {
results.add(Double.valueOf(builder.toString()));
}
return results;
}
public static void main(final String... args) {
System.out.printf("[numbers] %s", Playground.extract("12*2/5-3.0^3.23"))
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.