问题与十进制格式 [英] Problems with Decimalformat
本文介绍了问题与十进制格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用下面的代码来格式化我的数字有两位小数
$ b $ pre $ public String format(double num)
{
String temp =;
DecimalFormat df = new DecimalFormat(R。##);
temp = temp + df.format(num);
return temp;
$ / code $ / pre
当我打印出结果时,只给出一个小数点后的答案。
请有人可以帮我阻止。
解决方案
您可以使用#$ c
这个字符是打印数字,
$ b code $ DecimalFormat
您需要使用 0
,其中0也显示为0。
例如
DecimalFormat df = new DecimalFormat(R.00);
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."
You need to use 0
where 0 is also printed as 0.
E.g.
DecimalFormat df=new DecimalFormat("R.00");
这篇关于问题与十进制格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文