Java - 即使在零中也始终保留两位小数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20463196/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:07:48  来源:igfitidea点击:

Java - always keep two decimal places even in zeroes

javadecimalformat

提问by user3044874

I am trying to keep two decimal places, even if then numbers are zeroes, using DecimalFormatter:

我试图保留两位小数,即使数字为零,使用DecimalFormatter

DecimalFormat df = new DecimalFormat("#.00");

m_interest   = Double.valueOf(df.format(m_principal * m_interestRate));
m_newBalance = Double.valueOf(df.format(m_principal + m_interest - m_payment));
m_principal  = Double.valueOf(df.format(m_newBalance));

However for some values this gives two decimal places, and for others it doesnt. How can i fix this?

然而,对于某些值,这给出了两个小数位,而对于其他值,则没有。我怎样才能解决这个问题?

采纳答案by Michael Yaworski

It is because you are using Double.valueOfon the DecimalFormatand it is converting the formatted number back to a double, therefore eliminating the trailing 0s.

这是因为你正在使用Double.valueOfDecimalFormat,它是转换格式的数字回到了一倍,因此消除了拖尾0。

To fix this, only use the DecimalFormatwhen you are displayingthe value.

为了解决这个问题,只使用DecimalFormat,当你显示的值。

If you need m_interestcalculations, keep it as a regular double.

如果您需要m_interest计算,请将其保留为常规double.

Then when displaying, use:

然后在显示时使用:

System.out.print(df.format(m_interest));

Example:

例子:

DecimalFormat df = new DecimalFormat("#.00");
double m_interest = 1000;
System.out.print(df.format(m_interest)); // prints 1000.00

回答by Rob

Use BigDecimal instead, which supports the formatting approach you seek.

请改用 BigDecimal,它支持您寻求的格式化方法。

This question details it: How to print formatted BigDecimal values?

这个问题详细说明:如何打印格式化的 BigDecimal 值?

回答by Hovercraft Full Of Eels

DecimalFormat andNumberFormat should work just fine. A currency instance could work even better:

DecimalFormatNumberFormat 应该可以正常工作。货币实例可以更好地工作:

import java.text.DecimalFormat;
import java.text.NumberFormat;

public class Foo {
   public static void main(String[] args) {
      DecimalFormat df = new DecimalFormat("#0.00");

      NumberFormat nf = NumberFormat.getInstance();
      nf.setMinimumFractionDigits(2);
      nf.setMaximumFractionDigits(2);

      NumberFormat cf = NumberFormat.getCurrencyInstance();

      System.out.printf("0 with df is: %s%n", df.format(0));
      System.out.printf("0 with nf is: %s%n", nf.format(0));
      System.out.printf("0 with cf is: %s%n", cf.format(0));
      System.out.println();
      System.out.printf("12345678.3843 with df is: %s%n",
            df.format(12345678.3843));
      System.out.printf("12345678.3843 with nf is: %s%n",
            nf.format(12345678.3843));
      System.out.printf("12345678.3843 with cf is: %s%n",
            cf.format(12345678.3843));
   }
}

This would output:

这将输出:

0 with df is: 0.00
0 with nf is: 0.00
0 with cf is: 
m_interest = Double.valueOf(String.format("%.2f", sum));
.00 12345678.3843 with df is: 12345678.38 12345678.3843 with nf is: 12,345,678.38 12345678.3843 with cf is: ,345,678.38

回答by pinxue

Why don't you simply Math.round(value * 100) / 100.0 ?

你为什么不简单地 Math.round(value * 100) / 100.0 ?

回答by Daniel Gonzalez

##代码##