Issue
How can i convert example 1.07 into 7%, 1.50 into 50%, 2.25 into 125% e.t.c.
My code so far and it's not working:
private static final NumberFormat PERCENTAGE_FORMAT = NumberFormat.getInstance();
PERCENTAGE_FORMAT.setMaximumFractionDigits(0);
PERCENTAGE_FORMAT.format(1.07, "%")
Solution
You can do it like this:
double number = 1.15;
int percentage = (int) Math.round((number - 1) * 100);
System.out.println(percentage + "%"); // Output: 15%
To convert a decimal numbers to its percentage representation as a percentage of 1 you need to subtract 1 first.
Answered By - Ada
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.