简体   繁体   English

Decimalformat问题

[英]Problems with Decimalformat

Hey i am using the following code to format my numbers to have two decimal places 嘿,我正在使用以下代码来格式化我的数字,使其具有两位小数

public String format(double num)
{
    String temp="";
    DecimalFormat df=new DecimalFormat("R.##");
    temp=temp+df.format(num);
    return temp;
}

When I print out the results it gives me the answeres with only one decimal place. 当我打印出结果时,它给出的答案只有一位小数。 Please can someone help me stop that. 请有人帮我阻止那件事。

You use # in DecimalFormat which is the character that says "print the digit. if it is 0 leave it out." 您在DecimalFormat中使用# ,该字符表示“打印数字。如果为0,则忽略它”。

You need to use 0 where 0 is also printed as 0. 您需要使用0 ,其中0也被打印为0。

Eg 例如

DecimalFormat df=new DecimalFormat("R.00");
 BigDecimal bd = new BigDecimal(123.12331312);

System.out.print(bd.setScale(2,1));

try this 尝试这个

使用以下方式进行格式化

System.out.printf("%.2f",num);

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

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