我需要在Java中将float浮点到两位小数 [英] I need to round a float to two decimal places in Java
问题描述
我有困难四舍五入到小数点后两位。我已经尝试了一些我在这里看到的方法,包括简单地使用 Math.round()
,但是不管我做什么,我都会得到不寻常的数字。
我有一个正在处理的浮动列表,列表中的第一个显示为 1.2975118E7
。什么是 E7
?
当我使用 我知道我做错了什么,我只是不知道是什么。 / p> 我只是希望这些数字的格式是 另外, 你可以使用 Possible Duplicate: 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 I have a list of floats that I am processing, the first in the list is displayed as When I use I know I am doing something wrong, I just am not sure what. I just want the numbers to be in the format Also, You could use
这篇关于我需要在Java中将float浮点到两位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Math.round(f) code>(f是浮点数),我得到完全相同的数字。
x.xx
。第一个数字应该是 1.30
等等。
1.2975118E7
是科学记数法。
1.2975118E7 = 1.2975118 * 10 ^ 7 = 12975118
Math.round(f)
返回一个整数。你不能用它来得到你想要的格式 x.xx
。
String.format
。
字符串s = String.format(%。2f,1.2975118);
// 1.30
How to round a number to n decimal places in Java Math.round()
, but no matter what I do I keep getting unusual numbers.1.2975118E7
. What is the E7
?Math.round(f)
(f is the float), I get the exact same number.x.xx
. The first number should be 1.30
, etc.1.2975118E7
is scientific notation. 1.2975118E7 = 1.2975118 * 10^7 = 12975118
Math.round(f)
returns an integer. You can't use it to get your desired format x.xx
.String.format
.String s = String.format("%.2f", 1.2975118);
// 1.30