简体   繁体   English

格式化数字值从 XX.X 到 .XXX java

[英]Format number value from XX.X to .XXX java

I am receiving a value from a server that reads as a percentage, for example 77.5%.我从服务器接收一个以百分比形式读取的值,例如 77.5%。 How can get the value .775 just using:如何仅使用以下方法获得 .775 值:

String.format(Locale.getDefault(), formatPattern, floatValue);

I've consulted https://docs.oracle.com/javase/tutorial/java/data/numberformat.html , but it doesn't seem to cover my situation.我已经咨询过https://docs.oracle.com/javase/tutorial/java/data/numberformat.html ,但它似乎没有涵盖我的情况。

Note: I can only use the string format as we are placing the pattern into a config file.注意:我只能使用字符串格式,因为我们将模式放入配置文件中。

我们可以避免除以 100,只需使用以下方法将浮点移动到左两个位置:

double value = java.math.BigDecimal.valueOf(77.5).movePointLeft(2).doubleValue();

Try this:尝试这个:

String value = "75%";
Double aNumber = (Double.parseDouble(value.replace("%", "")))/100;

Does this work for you:这对你有用吗:

String num = "75.5%";
String res = new DecimalFormat("#.000").format(Double.parseDouble(num.substring(0, num.length()-1))/100);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM