java 在保持尾随零的同时舍入双精度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27832131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Round off a double while maintaining the trailing zero
提问by Nick Div
Here is my function to roundoff a number upto two decimals but when the rounded off number is 1.50 it seems to ignore the trailing zero and just returns 1.5
这是我将数字四舍五入到两位小数的函数,但是当四舍五入的数字为 1.50 时,它似乎忽略了尾随零而只返回 1.5
public static double roundOff(double number) {
double accuracy = 20;
number = number * accuracy;
number = Math.ceil(number);
number = number / accuracy;
return number;
}
So if I send 1.499 it returns 1.5 where as I want 1.50
因此,如果我发送 1.499,它将返回 1.5,而我想要的是 1.50
回答by Jean Logeart
This is a printing poblem:
这是一个打印问题:
double d = 1.5;
System.out.println(String.format("%.2f", d)); // 1.50
回答by paxdiablo
1.5
is, number of significant digits notwithstanding, the sameas 1.50
(and even 1.5000000000000
).
1.5
就是,尽管如此,显著位数相同的1.50
(甚至1.5000000000000
)。
You need to separate the valueof the number from its presentation.
您需要将数字的值与其表示分开。
If you want it output with two decimal digits, just use String.format
, such as with:
如果您希望它输出两位十进制数字,只需使用String.format
,例如:
public class Test
{
public static void main(String[] args) {
double d = 1.50000;
System.out.println(d);
System.out.println(String.format("%.2f", d));
}
}
which outputs:
输出:
1.5
1.50
If you still want a function that does all that for you andgives you a specific format, you'll need to return the string with something like:
如果您仍然想要一个为您完成所有工作并为您提供特定格式的函数,则需要使用以下内容返回字符串:
public static String roundOff(double num, double acc, String fmt) {
num *= acc;
num = Math.ceil(num);
num /= acc;
return String.format(fmt, num);
}
and call it with:
并调用它:
resultString = roundOff(value, 20, "%.2f"); // or 100, see below.
This will allow you to tailor the accuracy and output format in any way you desire, although you can still hard-code the values if you want simplicity:
这将允许您以任何您想要的方式定制准确性和输出格式,但如果您想要简单,您仍然可以对这些值进行硬编码:
public static String roundOff(double num) {
double acc = 20;
String fmt = "%.2f";
num *= acc;
num = Math.ceil(num);
num /= acc;
return String.format(fmt, num);
}
One final note: your question states that you want to round to "two decimals" but that doesn't quite gel with your use of 20
as the accuracy, since that will round it up to the next multiple of 1/20
. If you reallywant it rounded up to two decimals, the value you should be using for accuracy
is 100
.
最后一个注意事项:您的问题指出您想四舍五入到“两位小数”,但这与您的使用20
精度并不完全一致,因为这会将其四舍五入为. 如果你真的希望它四舍五入到两位小数,你应该使用的值是。1/20
accuracy
100
回答by Todd
You will have to format it as a String
in order to do that. Java, like most languages, will drop the trailing zero.
您必须将其格式化为 aString
才能做到这一点。与大多数语言一样,Java 将删除尾随零。
String.format("%.2f", number);
So you can either return a String
(change your return type from double) or just format it when you need to display it using the code above. You can read the JavaDoc for Formatterin order to understand all of the possibilities with number of decimal places, comma placement, etc.
因此,您可以返回 a String
(将返回类型从 double 更改)或仅在需要使用上面的代码显示时对其进行格式化。您可以阅读 JavaDoc for Formatter以了解小数位数、逗号放置等的所有可能性。
回答by Sreekanth
You can try this if you want is a String output
如果你想要一个字符串输出,你可以试试这个
double number = roundOff(1.499);//1.5
DecimalFormat decimalFormat = new DecimalFormat("#.00");
String fromattedDouble = decimalFormat.format(number);//1.50
The function roundOff is the same as you mentioned in your question.
函数 roundOff 与您在问题中提到的相同。