如何在Java中舍入到最接近的分数

时间:2014-09-29 22:48:42

标签: java

所以我正在编写一个程序来进行一些财务计算。但是,因为我的数据类型使用了double,所以美分不是四舍五入的。这是源代码:

public class CentRoundingTest {
    public static void main(String[] args) {
        System.out.println("TextLab03, Student Version\n");

        double principle = 259000;
        double annualRate = 5.75;
        double numYears = 30;

        // Calculates the number of total months in the 30 years which is the
        // number of monthly payments.
        double numMonths = numYears * 12;

        // Calculates the monthly interest based on the annual interest.
        double monthlyInterest = 5.75 / 100 / 12;

        // Calculates the monthly payment.
        double monthlyPayment = (((monthlyInterest * Math.pow(
                (1 + monthlyInterest), numMonths)) / (Math.pow(
                        (1 + monthlyInterest), numMonths) - 1)))
                        * principle;

        // calculates the total amount paid with interest for the 30 year time.
        // period.
        double totalPayment = monthlyPayment * numMonths;

        // Calculates the total interest that will accrue on the principle in 30
        // years.
        double totalInterest = monthlyPayment * numMonths - principle;

        System.out.println("Principle: $" + principle);
        System.out.println("Annual Rate: " + annualRate + "%");
        System.out.println("Number of years: " + numYears);
        System.out.println("Monthly Payment: $" + monthlyPayment);
        System.out.println("Total Payments: $" + totalPayment);
        System.out.println("Total Interest: $" + totalInterest);
    }
}

我的教师也不希望这个使用DecimalFormat类。我想通过这样做来获得美分值:variable-Math.floor(变量),然后将该数量四舍五入到最接近的百分之一,然后将它们加在一起。

2 个答案:

答案 0 :(得分:2)

不使用为此目的而存在的JDK提供的库类(并且通常会被使用),用于算术舍入的伪代码是:

  • 乘以100,给你分数
  • 添加(或减去数字为负数)0.5,所以下一步舍入到最近的分数
  • 强制转换为int,截断小数部分
  • 除以100d,给你钱)

现在去写一些代码。

答案 1 :(得分:0)

好吧,如果您无法使用DecimalFormat课程,可以使用printf()

TextLab03, Student Version

Principle       : $259,000.00
Annual Rate     : 5.75%
Number of years : 30.00
Monthly Payment : $1,511.45
Total Payments  : $544,123.33
Total Interest  : $285,123.33
public class CentRoundingTest {
    public static void main(String[] args) {
        System.out.println("TextLab03, Student Version\n");

        double principle = 259000;
        double annualRate = 5.75;
        double numYears = 30;

        // Calculates the number of total months in the 30 years which is the
        // number of monthly payments.
        double numMonths = numYears * 12;

        // Calculates the monthly interest based on the annual interest.
        double monthlyInterest = 5.75 / 100 / 12;

        // Calculates the monthly payment.
        double monthlyPayment = (((monthlyInterest * Math.pow(
                (1 + monthlyInterest), numMonths)) / (Math.pow(
                        (1 + monthlyInterest), numMonths) - 1)))
                        * principle;

        // calculates the total amount paid with interest for the 30 year time.
        // period.
        double totalPayment = monthlyPayment * numMonths;

        // Calculates the total interest that will accrue on the principle in 30
        // years.
        double totalInterest = monthlyPayment * numMonths - principle;

        printAmount("Principle", principle);
        printPercent("Annual Rate", annualRate);
        printCount("Number of years", numYears);
        printAmount("Monthly Payment", monthlyPayment);
        printAmount("Total Payments", totalPayment);
        printAmount("Total Interest", totalInterest);
    }

    public static void printPercent(String label, double percentage) {
        //System.out.printf("%-16s: %,.2f%%%n", label, percentage);
        printNumber(label, percentage, "", "%", 16);
    }

    public static void printCount(String label, double count) {
        //System.out.printf("%-16s: %,.2f%n", label, count);
        printNumber(label, count, "", "", 16);
    }

    public static void printAmount(String label, double amount) {
        //System.out.printf("%-16s: $%,.2f%n", label, amount);
        printNumber(label, amount, "$", "", 16);
    }

    public static void printNumber(String label, double value, String prefix, String suffix, int labelWidth) {
        String format = String.format("%%-%ds: %%s%%,.2f%%s%%n", labelWidth);
        System.out.printf(format, label, prefix, value, suffix);
    }
}