2013-09-22 65 views
8

我在编写一个简单的java程序。我需要从输入中获取一个字符串,并将其分为两部分:1双2字符串。 然后我需要对double进行简单的计算,并将结果以特定的精度发送到输出(4)。它工作正常,但输入为0时出现问题,则无法正常工作。在java中具有特定精度的双精度值

例如,对于这些输入,输出将是:

1公斤
输出:2.2046

3.1公斤
输出:6.8343

但是,当输入为0时,输出应为0.0000,但它显示为0.0。 我该如何强制显示0.0000?

我读到双精度类似的职位,他们认为像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"); 
+0

试试 “%0.4F”。这意味着'格式化一个浮点值,填充零直到你有4位数'。 – user268396

+0

我使用“%.4f”,但它显示此错误:异常在线程“main”java.util.MissingFormatWidthException:0.4f – MehrdadSComputer

+1

另请参见http://stackoverflow.com/questions/10959424/show-only-two-小数点后的数字,但这不是重复的,因为格式想要为0. – Raedwald

回答

18

DecimalFormat将允许您定义要多少位数字显示。即使值为零,'0'也会强制输出数字,而'#'将省略零。

System.out.print(new DecimalFormat("#0.0000").format(value)+" kg\n");应该诀窍。

documentation

注意:如果使用频繁,出于性能考虑,您应该实例格式化只有一次,存储参考:final DecimalFormat df = new DecimalFormat("#0.0000");。然后使用df.format(value)

+0

可以请你在代码中使用它,我不知道如何使用它! – MehrdadSComputer

+0

我已经做了,取代你的最后一行打印出你的价值。 – Blacklight

+0

它为我工作,但请更改为(“#0.0000”),您的答案输出为.0000,但如果我们使用(“#0.0000”),它显示0.0000,感谢您的帮助。 – MehrdadSComputer

0

使用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.

示例 -

System.out.print(new DecimalFormat("##.##").format(value)+" kg\n"); 
+0

您的答案和黑光的答案是我想要的,谢谢你的帮助。 – MehrdadSComputer

+0

@downvoter友善地评论negetive? –

0

String.format只是一个浮点值的字符串表示形式。如果它没有为最小精度提供标志,那么只需用零填充字符串的末尾。

+0

BlackLight的答案看起来更好。 – Aaron

+0

我使用了你的建议,但它不起作用,正如你所说的,BlackLight的答案就是我想要的,谢谢, – MehrdadSComputer

+0

我的答案对于“不工作”是不可能的:D它只是给字符串加“0”。尽管如此,这是次优解决方案。没有重新发明轮子的意思! – Aaron

4

的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 "#" 

然后,调用实例“四个一”,并通过你的双值显示时:

double value = 0; 
System.out.print(four.format(value) + " kg/n"); // displays 0.0000 
1

我建议你使用BigDecimal类来计算浮点值。您将能够控制浮点运算的精度。但是,回到正题:)

您可以使用下列内容:

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"); 
} 

会给你以下的输出:

0,0000 kg 

2,2046 kg 

6,8343 kg 
+0

感谢您的信息,我不知道如何使用bigDecimal设置精度。 – MehrdadSComputer