In java, how to convert percentage String to BigDecimal ?
Thanks
String percentage = "10%";
BigDecimal d ; // I want to get 0.1
We can convert a String into BigDecimal in Java using one of the below methods: BigDecimal(String) constructor. BigDecimal. valueOf() method.
It would be something like Double. valueOf(your_string) or Decimal. valueOf(your_string). If it is stored as whole number, you could convert it then divide by 100 to do math on it.
double decimalNumber = theVolume / 100.0; The . 0 at the end converts it to a decimal number.
Summary. To convert a percentage string to a decimal number use the built in function float with the replace string methods like so: float("30.0%". replace("%", ""))/100 .
Try new DecimalFormat("0.0#%").parse(percentage)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With