java中具有特定精度的双精度值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18946388/
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
Double value with specific precision in java
提问by Mehrdad Salimi
I'm programming a simple java program. I need to get a string from input and divide it into two parts: 1-double 2-string. Then I need to do a simple calculation on the double and send the result to the output with specific precision(4). It works fine, but there is a problem when the input is 0, then it doesn't work properly.
我正在编写一个简单的java程序。我需要从输入中获取一个字符串并将其分为两部分:1-double 2-string。然后我需要对 double 做一个简单的计算,并将结果发送到具有特定精度的输出(4)。工作正常,但输入为0时出现问题,则无法正常工作。
For example for these input, output will be:
例如对于这些输入,输出将是:
1 kg
output:2.2046
1公斤
产量:2.2046
3.1 kg
output:6.8343
3.1公斤
产量:6.8343
But when the input is 0, the output should be 0.0000, but it shows 0.0 . What should I do to force it to show 0.0000?
但是当输入为 0 时,输出应该是 0.0000,但它显示 0.0 。我该怎么做才能强制它显示 0.0000?
I read similar post about double precision, they suggest something like BigDecimal
class, but I can't use them in this case,
my code for doing this is:
我读过类似的关于双精度的帖子,他们建议类似BigDecimal
类的东西,但在这种情况下我不能使用它们,我这样做的代码是:
line=input.nextLine();
array=line.split(" ");
value=Double.parseDouble(array[0]);
type=array[1];
value =value*2.2046;
String s = String.format("%.4f", value);
value = Double.parseDouble(s);
System.out.print(value+" kg\n");
采纳答案by Blacklight
DecimalFormat
will allow you to define how many digits you want to display. A '0' will force an output of digits even if the value is zero, whereas a '#' will omit zeros.
DecimalFormat
将允许您定义要显示的数字数量。即使值为零,“0”也将强制输出数字,而“#”将省略零。
System.out.print(new DecimalFormat("#0.0000").format(value)+" kg\n");
should to the trick.
System.out.print(new DecimalFormat("#0.0000").format(value)+" kg\n");
应该诀窍。
See the documentation
查看文档
Note: if used frequently, for performance reasons you should instantiate the formatter only once and store the reference: final DecimalFormat df = new DecimalFormat("#0.0000");
. Then use df.format(value)
.
注意:如果经常使用,出于性能原因,您应该只将格式化程序实例化一次并存储引用:final DecimalFormat df = new DecimalFormat("#0.0000");
。然后使用df.format(value)
.
回答by Subhrajyoti Majumder
Use DecimalFormat
to format your double value to fixed precision string output.
用于DecimalFormat
将双精度值格式化为固定精度字符串输出。
DecimalFormat is a concrete subclass of NumberFormat that formats decimal numbers. It has a variety of features designed to make it possible to parse and format numbers in any locale, including support for Western, Arabic, and Indic digits. It also supports different kinds of numbers, including integers (123), fixed-point numbers (123.4), scientific notation (1.23E4), percentages (12%), and currency amounts ($123). All of these can be localized.
DecimalFormat 是 NumberFormat 的具体子类,用于格式化十进制数。它具有多种功能,旨在使解析和格式化任何语言环境中的数字成为可能,包括对西方、阿拉伯和印度数字的支持。它还支持不同种类的数字,包括整数 (123)、定点数 (123.4)、科学记数法 (1.23E4)、百分比 (12%) 和货币金额 ($123)。所有这些都可以本地化。
Example -
例子 -
System.out.print(new DecimalFormat("##.##").format(value)+" kg\n");
回答by Aaron
String.format is just makign a String representation of the floating point value. If it doesnt provide a flag for a minimum precision, then just pad the end of the string with zeros.
String.format 只是浮点值的字符串表示。如果它没有提供最小精度的标志,那么只需用零填充字符串的末尾。
回答by Michael Yaworski
add this instance of DecimalFormat to the top of your method:
将此 DecimalFormat 实例添加到方法的顶部:
DecimalFormat four = new DecimalFormat("#0.0000"); // will round and display the number to four decimal places. No more, no less.
// the four zeros after the decimal point above specify how many decimal places to be accurate to.
// the zero to the left of the decimal place above makes it so that numbers that start with "0." will display "0.____" vs just ".____" If you don't want the "0.", replace that 0 to the left of the decimal point with "#"
then, call the instance "four" and pass your double value when displaying:
然后,调用实例“four”并在显示时传递双值:
double value = 0;
System.out.print(four.format(value) + " kg/n"); // displays 0.0000
回答by Ortwin Angermeier
I suggest you to use the BigDecimal
class for calculating with floating point values. You will be able to control the precision of the floating point arithmetic. But back to the topic :)
我建议您使用BigDecimal
该类来计算浮点值。您将能够控制浮点运算的精度。但回到主题:)
You could use the following:
您可以使用以下内容:
static void test(String stringVal) {
final BigDecimal value = new BigDecimal(stringVal).multiply(new BigDecimal("2.2046"));
DecimalFormat df = new DecimalFormat();
df.setMaximumFractionDigits(4);
df.setMinimumFractionDigits(4);
System.out.println(df.format(value) + " kg\n");
}
public static void main(String[] args) {
test("0");
test("1");
test("3.1");
}
will give you the following output:
将为您提供以下输出:
0,0000 kg
2,2046 kg
6,8343 kg
回答by Marc Dzaebel
System.out.format("%.4f kg\n", 0.0d)
prints '0.0000 kg'
System.out.format("%.4f kg\n", 0.0d)
打印 '0.0000 kg'