java - 学校项目,四舍五入到最接近的分。

标签 java

我正在为类做一个贷款计算器。我快完成了,但唯一缺少的是如何四舍五入 INTEREST_RATE 而不是 $298.95833333333337 我想得到 $298.96。但我不知道怎么办。

输入您的旧原则 = 25000
输入您当前的付款= 450

之前余额:$25000.0
付款方式:$450.0
支付利息:$151.04166666666666
已付本金:$298.95833333333337
新本金:$24701.041666666668

import java.util.Scanner; 

public class LoanCalculator {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    /**
     * Declaration Section  
     * 
     */
    Scanner keyboard = new Scanner(System.in); 
    double INTEREST_RATE; 
    double currentPayment;
    double oldPrincipal;
    double interestPaid;
    double principalPaid;
    double newPrincipal; 


    /**
     * Process Section
     * 
     */

    System.out.println("Enter your old Principle = ");
    oldPrincipal = keyboard.nextDouble(); 

    System.out.println("Enter your current payment = ");
    currentPayment = keyboard.nextDouble();  

    INTEREST_RATE = 7.25 / 100.0; //fix this 

    interestPaid = oldPrincipal * INTEREST_RATE / 12;

    principalPaid = currentPayment - interestPaid; 

    newPrincipal = oldPrincipal - principalPaid; 


    /**
     * Output Section
     * 
     * */
    System.out.println("Previous Balance: " + "$"+ oldPrincipal);
    System.out.println("Payment: " + "$"+ currentPayment);
    System.out.println("Interest Paid: " + "$"+ interestPaid);
    System.out.println("Principle Paid: " + "$"+ principalPaid);
    System.out.println("New Principal: " + "$"+ newPrincipal); 


}//Main()

}//LoanCalculator

最佳答案

切勿使用double进行财务计算!这是really dangerous .

摘自Java语言规范:

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other useful classes provided by the Java platform.

double: The double data type is a double-precision 64-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, this data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

使用BigDecimal ,它还具有您正在寻找的舍入功能。

关于java - 学校项目,四舍五入到最接近的分。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26067893/

相关文章:

java - 检测打开我的 JavaFX 程序的文件

Java CMS 被忽略,取而代之的是 Full GC

java - 无法从 Android 设备连接 Tomcat

java - JDA 语音 Activity 跟踪

java - 每行打印 10 个数字

java - 延迟加载和 Hibernate.initialize() 的问题;

java - 我的 log4j SiftAppender 不生成文件

java - iText PdfCopy 创建可编辑的 pdf 文档

java - 具有 newrelic java 代理和supervisord 的 Docker 容器在首次启动时不发送数据

java - 方法 : java. lang.String.div() 的签名不适用于参数类型 : (java. lang.Integer)