Possible Duplicate:
Round a double to 2 significant figures after decimal point可能重复:小数点后的两位数到两位数
I know that there are plenty of examples on how to round this kind numbers. But could someone show me how to round double, to get value that I can display as a String and ALWAYS have 2 decimal places?
我知道有很多关于如何四舍五入的例子。但是有人能教我怎么用双精度来表示值吗?我可以用字符串来表示,并且总是有2个小数点吗?
2 个解决方案
#1
352
You can use String.format("%.2f", d)
, your double will be rounded automatically.
您可以使用String.format(“%。2f, d),你的双份将自动完成。
#2
9
One easy way to do it:
一个简单的方法是:
Double d;
Int i;
D+=0.005;
i=d*100;
Double b = i/100;
String s = b.toString():
#1
352
You can use String.format("%.2f", d)
, your double will be rounded automatically.
您可以使用String.format(“%。2f, d),你的双份将自动完成。
#2
9
One easy way to do it:
一个简单的方法是:
Double d;
Int i;
D+=0.005;
i=d*100;
Double b = i/100;
String s = b.toString():