【问题标题】:Multiply with two Decimal Place - Java乘以两个小数位 - Java
【发布时间】:2015-04-02 06:40:39
【问题描述】:

将双精度类型数相乘,输出需要取两个小数位

double creditCardPercentage = 0.03;
String renewalEventAmount = "2144.60";
double expectedRenewalAmount = 0;
expectedRenewalAmount = Double.parseDouble(renewalEventAmount) * creditCardPercentage;

expectedRenewalAmount 的输出是64.338,那么我们如何将上面的输出格式化为两位小数

预期:

64.33

【问题讨论】:

  • @Prabu 您是否只关心输出的格式,或者,按照您给出的示例,您是否会对输出进行进一步的数学运算?
  • 在处理货币时,使用双重算术通常不是一个好主意,因为它不精确。最好将它们转换为整数(乘以 10 的倍数,然后除以 10 的倍数,具体取决于您想要的小数位数。

标签: java


【解决方案1】:

您应该使用BigDecimal 类,因为它具有对浮点​​精度的内置处理:

BigDecimal creditCardPercentage = new BigDecimal(0.03);
String renewalEventAmountString = "2144.60";
BigDecimal renewalEventAmount = new BigDecimal(renewalEventAmountString);
BigDecimal expectedRenewalAmount = renewalEventAmount.multiply(creditCardPercentage);
expectedRenewalAmount = expectedRenewalAmount.setScale(2, BigDecimal.ROUND_HALF_DOWN);

System.out.println(expectedRenewalAmount); // prints 64.33

使用BigDecimal 处理格式的优点之一是它允许您将代码与业务逻辑分开,即从将结果输出给用户的视图代码中舍入到小数点后两位。

【讨论】:

    【解决方案2】:

    您可以使用NumberFormat.getNumberInstance().format(expectedRenewalAmount),它将使用系统属性来格式化值。您还可以修改NumberFormat,根据需要指定小数位数。

    或者您也可以使用System.out.printfString.format 来格式化值...

    String value = String.format("%.2f", expectedRenewalAmount);
    

    【讨论】:

      【解决方案3】:

      有几个答案建议printf;但如果你不想马上打印答案,你可以用同样的方式使用String.format

      String formattedString = String.format("%.2f", expectedRenewalAmount);
      

      现在您可以根据需要打印出结果,或者您可以在 Swing 文本窗口中显示它,或者将所有零更改为快乐的面孔,或者对生成的字符串做任何您喜欢的事情,这是您无法做到的printf

      【讨论】:

        【解决方案4】:

        您可以将printf() 函数与%f 一起使用:

        System.out.printf("%.2f", expectedRenewalAmount);
        

        在这里,您可以找到 Alvin Alexander 撰写的精美 printf format cheat sheet,它可能对您(以及希望其他人)有很大帮助。

        【讨论】:

          【解决方案5】:

          打印时试试这个:

          System.out.printf("%.2f", expectedRenewalAmount);
          

          【讨论】:

            【解决方案6】:

            你可以使用

            DecimalFormat df = new DecimalFormat("#.00");
            double creditCardPercentage = 0.03;
            String renewalEventAmount = "2144.60";
            double expectedRenewalAmount = 0;
            expectedRenewalAmount = df.format(Double.parseDouble(renewalEventAmount) * creditCardPercentage);
            

            【讨论】:

              【解决方案7】:

              你可以用minimumFractionDigit转0

              public class Test {
              
                  public static void main(String[] args) {
                      System.out.println(format(14.0184849945)); // prints '14.01'
                      System.out.println(format(13)); // prints '13'
                      System.out.println(format(3.5)); // prints '3.5'
                      System.out.println(format(3.138136)); // prints '3.13'
                  }
              
                  public static String format(Number n) {
                      NumberFormat format = DecimalFormat.getInstance();
                      format.setRoundingMode(RoundingMode.FLOOR);
                      format.setMinimumFractionDigits(0);
                      format.setMaximumFractionDigits(2);
                      return format.format(n);
                  }
              
              }
              

              请在下面找到参考链接。

              Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2015-10-17
                • 2012-11-07
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-03-13
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多