当我在做数学时,Java总是想要四舍五入

时间:2014-02-17 04:12:09

标签: java

问题正在发生,我想,程序使用的是利率。 使用测试数据: 贷款金额:$ 500,000.00 年利率:5.6% 年数:30

我应该得到结果: 每月付款:2,870.39美元

相反,我得到: 贷款金额:$ 500,000.00 年利率:6% 年数:30.0 每月付款:34,783.54美元

请帮助

编辑 - 这是一个家庭作业,教授已经注意到他不希望我们使用BigDecimal对象。为什么超出我的范围,因为我已经证明了我过去使用BigDecimal数学解决这个问题的能力。

以下是代码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package workbook5.pkg2;
import java.util.Scanner;
import java.text.*;
/**
 *
 * @author Daniel
 */
public class Workbook52 {

    /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    double loanAmount = getDouble(sc, "Enter loan amount: ", 0, 1000000);
    double interestRate = getDouble(sc, "Enter interest rate (5% as .05): ", 0, 20);
    double years = getInt(sc, "Enter amount of years: ", 0, 100);
    double payment = calculatePayment(loanAmount, interestRate, years);
    System.out.println("FORMATTED RESULTS");
    System.out.println("Loan amount:          " + NumberFormat.getCurrencyInstance().format(loanAmount));
    System.out.println("Yearly interest rate: " + NumberFormat.getPercentInstance().format(interestRate));
    System.out.println("Number of years:      " + years);
    System.out.println("Monthly Payment:      " + NumberFormat.getCurrencyInstance().format(payment));
}
public static double getDouble(Scanner sc, String prompt, double min, double max){
    double i = 0;
    boolean isValid = false;
    while (isValid == false)
    {    
        System.out.println(prompt);
        if (sc.hasNextDouble())
        {
          i = sc.nextDouble();
          isValid=true;
        }


        else
        {
            System.out.println("Error! Invalid double value. Try again.");

        }
        sc.nextLine();
        if (isValid == true && i <= min)
        {
            System.out.println("Error: Number must be greater than " + min);
            isValid = false;
        }
        else if (isValid == true && i >= max)
        {
            System.out.println("Error: Number must be less than " + max);
            isValid = false;
        }
    }
    return i;
}

public static int getInt(Scanner sc, String prompt, int min, int max){
    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.println(prompt);
        if (sc.hasNextInt())
        {
          i = sc.nextInt();
          isValid=true;
        }        
        else
        {
            System.out.println("Error! Invalid integer value. Try again.");

        }
        sc.nextLine();
        if (isValid == true && i <= min)
        {
            System.out.println("Error: Number must be greater than " + min);
            isValid = false;
        }
        else if (isValid == true && i >= max)
        {
            System.out.println("Error: Number must be less than " + max);
            isValid = false;
        }
    }    

    return i;
    }






 public static double calculatePayment(double amount, double interest, double years) {
       double monthlyPayment = amount * interest/(1 - 1/Math.pow(1 + interest, years));
       return monthlyPayment;
   }
}

3 个答案:

答案 0 :(得分:1)

每当你在java中处理钱时,你使用的数据类型是BigDecimal而不是float或double。修改您的程序以使用BigDecimal而不是double,您应该得到正确和预期的结果。

答案 1 :(得分:1)

如果以美分存储和计算所有货币值,您可以安全地使用int(或long非常大的金额)。

银行业(尤其是EFTPOS)使用美分,如果它对他们来说足够好,那么对你来说也应该没问题。

您只能使用货币符号将数字作为十进制数字呈现

答案 2 :(得分:0)

程序中的舍入没有任何问题。公式的输入是错误的。您使用年度利率和的数量来计算月度付款。您需要转换为每月费率和条款数量。试试这个:

public static double calculatePayment(double amount, double interest, double years) {
    double monthlyInterest = interest / 12.0;
    double terms = years * 12.0;
    double monthlyPayment = amount * monthlyInterest
            / (1 - 1 / Math.pow(1 + monthlyInterest, terms));
    return monthlyPayment;
}