Reputation: 8362
I am receiving a value from a server that reads as a percentage, for example 77.5%. How can get the value .775 just using:
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.
Note: I can only use the string format as we are placing the pattern into a config file.
Upvotes: 1
Views: 11119
Reputation: 1938
Does this work for you:
String num = "75.5%";
String res = new DecimalFormat("#.000").format(Double.parseDouble(num.substring(0, num.length()-1))/100);
Upvotes: 0
Reputation: 11
We can avoid the division by 100 and just moving the floating point to left two places by using :
double value = java.math.BigDecimal.valueOf(77.5).movePointLeft(2).doubleValue();
Upvotes: 1
Reputation: 10184
Try this:
String value = "75%";
Double aNumber = (Double.parseDouble(value.replace("%", "")))/100;
Upvotes: 0