convert double to percent in java 1.5 = 50%

i have this code, variables first, second and third only can obtain in double i need format in percent to write later on other place

import java.text.DecimalFormat;

public class Main {
public static void main(String[] argv) throws Exception {

double first = 0.5;
double second = 1.5;
double third = 2.5;

DecimalFormat df = new DecimalFormat("#%");
System.out.println(df.format(first));
System.out.println(df.format(second));
System.out.println(df.format(third));
}
}

output: 50% 150% 250%

i need obtain this result

-50%
50%
150%

i hope any can help me with this thanks

Upvotes: 0

Views: 635

Answers (1)

Robby Cornelissen
Robby Cornelissen

Reputation: 97227

I looks like you need values relative to 100%, so you just need to subtract 1 (i.e. 100%) from your values:

System.out.println(df.format(first - 1));
System.out.println(df.format(second - 1));
System.out.println(df.format(third - 1));

Upvotes: 2

Related Questions