I have a string value as below:
String percValue = "0.0209"
How can I convert this to something like as below
String formatedValue = "2.09%";
Can someone help me what is the simple and best way to convert this?
I have a string value as below:
String percValue = "0.0209"
How can I convert this to something like as below
String formatedValue = "2.09%";
Can someone help me what is the simple and best way to convert this?
One good way would be to:
double
type variable, so it can hold the decimal places...),String percValue = "0.0209";
double percentage = Double.parseDouble(percValue) * 100;
String formattedValue = String.format("%.2f%%", percentage);
Explanation:
Double.parseDouble()
takes your string as a parameter and returns a double value which you can do things like multiplication and addition with, and String.format()
lets you precisely control how your number is converted back to a String! You should parse the String
into a double
, multiply by 100, and then append the %
sign to it as follows:
String percValue = "0.0209";
double per = Double.parseDouble(percValue);
String percentage = (per*100)+"%";
You need to parse your string value and then multiply by 100, something like this:
String percValue = "0.0209";
double value = ( Double.parseDouble(percValue)) * 100;
String formatedValue = value + "%";
Convert String to BigDecimal(for Big numbers) and multiply by 100.
String percValue = "0.0209";
BigDecimal temp=new BigDecimal(percValue).multiply(BigDecimal.valueOf(100)).stripTrailingZeros();
String formatedValue =temp.toString() + "%";