Possible Duplicate:
How to round a number to n decimal places in Java可能重复:如何在Java中将数字舍入到n个小数位
I am having difficulties rounding a float to two decimal places. I have tried a few methods I have seen on here including simply just using Math.round()
, but no matter what I do I keep getting unusual numbers.
我很难将浮点数舍入到小数点后两位。我已经尝试了一些我在这里看到的方法,包括简单地使用Math.round(),但无论我做什么,我都会得到不寻常的数字。
I have a list of floats that I am processing, the first in the list is displayed as 1.2975118E7
. What is the E7
?
我有一个我正在处理的浮动列表,列表中的第一个显示为1.2975118E7。什么是E7?
When I use Math.round(f)
(f is the float), I get the exact same number.
当我使用Math.round(f)(f是浮点数)时,我得到完全相同的数字。
I know I am doing something wrong, I just am not sure what.
我知道我做错了什么,我只是不确定是什么。
I just want the numbers to be in the format x.xx
. The first number should be 1.30
, etc.
我只想要数字格式为x.xx.第一个数字应该是1.30等
4 个解决方案
#1
83
1.2975118E7
is scientific notation.
1.2975118E7是科学记数法。
1.2975118E7 = 1.2975118 * 10^7 = 12975118
Also, Math.round(f)
returns an integer. You can't use it to get your desired format x.xx
.
此外,Math.round(f)返回一个整数。您不能使用它来获得所需的格式x.xx.
You could use String.format
.
你可以使用String.format。
String s = String.format("%.2f", 1.2975118);
// 1.30
#2
44
If you're looking for currency formatting (which you didn't specify, but it seems that is what you're looking for) try the NumberFormat
class. It's very simple:
如果您正在寻找货币格式(您没有指定,但似乎这是您正在寻找的),请尝试使用NumberFormat类。这很简单:
double d = 2.3d;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String output = formatter.format(d);
Which will output (depending on locale):
哪个将输出(取决于区域设置):
$2.30
Also, if currency isn't required (just the exact two decimal places) you can use this instead:
此外,如果不需要货币(只是精确的两位小数),您可以使用此代码:
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String output = formatter.format(d);
Which will output 2.30
哪个会输出2.30
#3
6
You can make use of DecimalFormat
to give you the style you wish.
您可以使用DecimalFormat为您提供所需的样式。
DecimalFormat df = new DecimalFormat("0.00E0");
double number = 1.2975118E7;
System.out.println(df.format(number)); // prints 1.30E7
Since it's in scientific notation, you won't be able to get the number any smaller than 107 without losing that many orders of magnitude of accuracy.
由于它采用科学记数法,因此在不损失许多数量级精度的情况下,您将无法获得小于107的数字。
#4
-2
Try looking at the BigDecimal Class. It is the go to class for currency and support accurate rounding.
尝试查看BigDecimal类。这是货币的上课和支持准确的舍入。
#1
83
1.2975118E7
is scientific notation.
1.2975118E7是科学记数法。
1.2975118E7 = 1.2975118 * 10^7 = 12975118
Also, Math.round(f)
returns an integer. You can't use it to get your desired format x.xx
.
此外,Math.round(f)返回一个整数。您不能使用它来获得所需的格式x.xx.
You could use String.format
.
你可以使用String.format。
String s = String.format("%.2f", 1.2975118);
// 1.30
#2
44
If you're looking for currency formatting (which you didn't specify, but it seems that is what you're looking for) try the NumberFormat
class. It's very simple:
如果您正在寻找货币格式(您没有指定,但似乎这是您正在寻找的),请尝试使用NumberFormat类。这很简单:
double d = 2.3d;
NumberFormat formatter = NumberFormat.getCurrencyInstance();
String output = formatter.format(d);
Which will output (depending on locale):
哪个将输出(取决于区域设置):
$2.30
Also, if currency isn't required (just the exact two decimal places) you can use this instead:
此外,如果不需要货币(只是精确的两位小数),您可以使用此代码:
NumberFormat formatter = NumberFormat.getNumberInstance();
formatter.setMinimumFractionDigits(2);
formatter.setMaximumFractionDigits(2);
String output = formatter.format(d);
Which will output 2.30
哪个会输出2.30
#3
6
You can make use of DecimalFormat
to give you the style you wish.
您可以使用DecimalFormat为您提供所需的样式。
DecimalFormat df = new DecimalFormat("0.00E0");
double number = 1.2975118E7;
System.out.println(df.format(number)); // prints 1.30E7
Since it's in scientific notation, you won't be able to get the number any smaller than 107 without losing that many orders of magnitude of accuracy.
由于它采用科学记数法,因此在不损失许多数量级精度的情况下,您将无法获得小于107的数字。
#4
-2
Try looking at the BigDecimal Class. It is the go to class for currency and support accurate rounding.
尝试查看BigDecimal类。这是货币的上课和支持准确的舍入。