简体   繁体   English

在 java 1.5 中将 double 转换为百分比 = 50%

[英]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%输出:50% 150% 250%

i need obtain this result我需要得到这个结果

-50%
50%
150%

i hope any can help me with this thanks我希望任何人都可以帮助我,谢谢

I looks like you need values relative to 100%, so you just need to subtract 1 (ie 100%) from your values:我看起来你需要相对于 100% 的值,所以你只需要从你的值中减去 1(即 100%):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM