Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用小数点显示百分比的格式_Java - Fatal编程技术网

Java 用小数点显示百分比的格式

Java 用小数点显示百分比的格式,java,Java,我需要一些关于如何以“.5%”格式获得我的第二个和第四个百分比的技巧,因为在我的结果中,我得到了正确的结果,但它以整个百分比打印。因此,它给出的不是4%、4.5%、5%、5.5%和6%。4%、4%、5%、6%、6%,但仍能正确计算结果。它好像没有显示4.5%和5.5%。这里的代码,它可能看起来很长,但它只是重复的数字。注意aIR2和aIR4,它们是目标 import java.util.Scanner; import java.text.NumberFormat; import java.tex

我需要一些关于如何以“.5%”格式获得我的第二个和第四个百分比的技巧,因为在我的结果中,我得到了正确的结果,但它以整个百分比打印。因此,它给出的不是4%、4.5%、5%、5.5%和6%。4%、4%、5%、6%、6%,但仍能正确计算结果。它好像没有显示4.5%和5.5%。这里的代码,它可能看起来很长,但它只是重复的数字。注意aIR2和aIR4,它们是目标

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;

public class MortgageCalculator {

    public static void main(String[] args) {
        double aIR, mortgageAmount;
        int noY;
        double aIR1, aIR2, aIR3, aIR4, aIR5;
        double mIR1, mIR2, mIR3, mIR4, mIR5;
        double mPayment1, mPayment2, mPayment3, mPayment4, mPayment5;
        double totAmount1, totAmount2, totAmount3, totAmount4, totAmount5;
        double oPay1, oPay2, oPay3, oPay4, oPay5;
        double oPaypercent1, oPaypercent2, oPaypercent3, oPaypercent4, oPaypercent5;

        //This establishes formats for values
        NumberFormat fmt1 = NumberFormat.getPercentInstance();
        NumberFormat fmt2 = NumberFormat.getCurrencyInstance();
        DecimalFormat fmt3 = new DecimalFormat("0.##");

        //This asks user for inputs
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the Annual Interest Rate: ");
        aIR= scan.nextDouble();
        System.out.println("Enter the Number of Years you will Pay: ");
        noY = scan.nextInt();
        System.out.println("Enter Amount Borrowed from the Bank: ");
        mortgageAmount = scan.nextInt();

        //These will give annual interest rates depending on range
        aIR1 = (aIR - 1)/100;
        aIR2 = (aIR - 0.5)/100;
        aIR3 = (aIR)/100;
        aIR4 = (aIR + 0.5)/100;
        aIR5 = (aIR + 1)/100;

        //These give rates by month according to which rate being taken
        mIR1 = (aIR1 / 12);
        mIR2 = (aIR2 / 12);
        mIR3 = (aIR3 / 12);
        mIR4 = (aIR4 / 12);
        mIR5 = (aIR5 / 12);

        //This takes the amounts per month
        mPayment1 = (mIR1 * mortgageAmount)/(1-(1/Math.pow((1+mIR1),12*noY)));
        mPayment2 = (mIR2 * mortgageAmount)/(1-(1/Math.pow((1+mIR2),12*noY)));
        mPayment3 = (mIR3 * mortgageAmount)/(1-(1/Math.pow((1+mIR3),12*noY)));
        mPayment4 = (mIR4 * mortgageAmount)/(1-(1/Math.pow((1+mIR4),12*noY)));
        mPayment5 = (mIR5 * mortgageAmount)/(1-(1/Math.pow((1+mIR5),12*noY)));

        //This takes the total amount per year
        totAmount1 = mPayment1 * (noY*12);
        totAmount2 = mPayment2 * (noY*12);
        totAmount3 = mPayment3 * (noY*12);
        totAmount4 = mPayment4 * (noY*12);
        totAmount5 = mPayment5 * (noY*12);

        //This is the overpayment because of interest
        oPay1 = totAmount1 - (mortgageAmount);
        oPay2 = totAmount2 - (mortgageAmount);
        oPay3 = totAmount3 - (mortgageAmount);
        oPay4 = totAmount4 - (mortgageAmount);
        oPay5 = totAmount5 - (mortgageAmount);

        //This is the overpayment percentage
        oPaypercent1 = (oPay1/mortgageAmount);
        oPaypercent2 = (oPay2/mortgageAmount);
        oPaypercent3 = (oPay3/mortgageAmount);
        oPaypercent4 = (oPay4/mortgageAmount);
        oPaypercent5 = (oPay5/mortgageAmount);

        //Begins printing the results in a line
        System.out.println("The Mortgage Amount is: " 
                + fmt2.format(mortgageAmount));
        System.out.println("The Number of Years the Mortgage is Held: " + noY);
        System.out.println("Range of Interest Rates: " + fmt1.format(aIR1) 
                + " - " + fmt1.format(aIR5));
        System.out.println("Interest Rate   Monthly Payment   Total Payment"
                + "   $ Overpayment   % Overpayment");

        //Prints first interest rate
        System.out.println("" + fmt1.format(aIR1) + "               "  +  
                fmt2.format(mPayment1) + "            " + fmt2.format(totAmount1)
                + "       " + fmt2.format(oPay1) + "          " 
                + fmt1.format(oPaypercent1) + "");

        //Prints second interest rate
        System.out.println("" + fmt1.format(aIR2) + "               "  +  
                fmt2.format(mPayment2) + "            " + fmt2.format(totAmount2)
                + "       " + fmt2.format(oPay2) + "          " 
                + fmt1.format(oPaypercent2) + "");

        //Prints third interest rate
        System.out.println("" + fmt1.format(aIR3) + "               "  +  
                fmt2.format(mPayment3) + "            " + fmt2.format(totAmount3)
                + "       " + fmt2.format(oPay3) + "          " 
                + fmt1.format(oPaypercent3) + "");

        //Prints fourth interest rate
        System.out.println("" + fmt1.format(aIR4) + "               "  +  
                fmt2.format(mPayment4) + "            " + fmt2.format(totAmount4)
                + "       " + fmt2.format(oPay4) + "          " 
                + fmt1.format(oPaypercent4) + "");

        //Prints fifth interest rate
        System.out.println("" + fmt1.format(aIR5) + "               "  +  
                fmt2.format(mPayment5) + "            " + fmt2.format(totAmount5)
                + "       " + fmt2.format(oPay5) + "          " 
                + fmt1.format(oPaypercent5) + "");
    }

}

如果需要显示
4%
4.5%
NumberFormat
,即带有单个可选小数位数的百分比值,请执行以下操作之一:

// Option 1
NumberFormat fmt = NumberFormat.getPercentInstance();
fmt.setMaximumFractionDigits(1);

// Option 2
NumberFormat fmt = new DecimalFormat("0.#%");

如果需要显示
4%
4.5%
NumberFormat
,即带有单个可选小数位数的百分比值,请执行以下操作之一:

// Option 1
NumberFormat fmt = NumberFormat.getPercentInstance();
fmt.setMaximumFractionDigits(1);

// Option 2
NumberFormat fmt = new DecimalFormat("0.#%");

应更改利率格式以处理十进制百分比值

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;

public class HelloWorld {

    public static void main(String[] args) {
        double aIR, mortgageAmount;
        int noY;
        double aIR1, aIR2, aIR3, aIR4, aIR5;
        double mIR1, mIR2, mIR3, mIR4, mIR5;
        double mPayment1, mPayment2, mPayment3, mPayment4, mPayment5;
        double totAmount1, totAmount2, totAmount3, totAmount4, totAmount5;
        double oPay1, oPay2, oPay3, oPay4, oPay5;
        double oPaypercent1, oPaypercent2, oPaypercent3, oPaypercent4, oPaypercent5;

        //This establishes formats for values
        NumberFormat fmt1 = new DecimalFormat("0.#%");
        //NumberFormat fmt1 = NumberFormat.getPercentInstance();
        NumberFormat fmt2 = NumberFormat.getCurrencyInstance();
        DecimalFormat fmt3 = new DecimalFormat("0.##");

        //This asks user for inputs
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the Annual Interest Rate: ");
        aIR= scan.nextDouble();
        System.out.println("Enter the Number of Years you will Pay: ");
        noY = scan.nextInt();
        System.out.println("Enter Amount Borrowed from the Bank: ");
        mortgageAmount = scan.nextInt();

        //These will give annual interest rates depending on range
        aIR1 = (aIR - 1)/100;
        aIR2 = (aIR - 0.5)/100;
        aIR3 = (aIR)/100;
        aIR4 = (aIR + 0.5)/100;
        aIR5 = (aIR + 1)/100;

        //These give rates by month according to which rate being taken
        mIR1 = (aIR1 / 12);
        mIR2 = (aIR2 / 12);
        mIR3 = (aIR3 / 12);
        mIR4 = (aIR4 / 12);
        mIR5 = (aIR5 / 12);

        //This takes the amounts per month
        mPayment1 = (mIR1 * mortgageAmount)/(1-(1/Math.pow((1+mIR1),12*noY)));
        mPayment2 = (mIR2 * mortgageAmount)/(1-(1/Math.pow((1+mIR2),12*noY)));
        mPayment3 = (mIR3 * mortgageAmount)/(1-(1/Math.pow((1+mIR3),12*noY)));
        mPayment4 = (mIR4 * mortgageAmount)/(1-(1/Math.pow((1+mIR4),12*noY)));
        mPayment5 = (mIR5 * mortgageAmount)/(1-(1/Math.pow((1+mIR5),12*noY)));

        //This takes the total amount per year
        totAmount1 = mPayment1 * (noY*12);
        totAmount2 = mPayment2 * (noY*12);
        totAmount3 = mPayment3 * (noY*12);
        totAmount4 = mPayment4 * (noY*12);
        totAmount5 = mPayment5 * (noY*12);

        //This is the overpayment because of interest
        oPay1 = totAmount1 - (mortgageAmount);
        oPay2 = totAmount2 - (mortgageAmount);
        oPay3 = totAmount3 - (mortgageAmount);
        oPay4 = totAmount4 - (mortgageAmount);
        oPay5 = totAmount5 - (mortgageAmount);

        //This is the overpayment percentage
        oPaypercent1 = (oPay1/mortgageAmount);
        oPaypercent2 = (oPay2/mortgageAmount);
        oPaypercent3 = (oPay3/mortgageAmount);
        oPaypercent4 = (oPay4/mortgageAmount);
        oPaypercent5 = (oPay5/mortgageAmount);

        //Begins printing the results in a line
        System.out.println("The Mortgage Amount is: " 
                + fmt2.format(mortgageAmount));
        System.out.println("The Number of Years the Mortgage is Held: " + noY);
        System.out.println("Range of Interest Rates: " + fmt1.format(aIR1) 
                + " - " + fmt1.format(aIR5));
        System.out.println("Interest Rate   Monthly Payment   Total Payment"
                + "   $ Overpayment   % Overpayment");

        //Prints first interest rate
        System.out.println("" + fmt1.format(aIR1) + "               "  +  
                fmt2.format(mPayment1) + "            " + fmt2.format(totAmount1)
                + "       " + fmt2.format(oPay1) + "          " 
                + fmt1.format(oPaypercent1) + "");

        //Prints second interest rate
        System.out.println("" + fmt1.format(aIR2) + "               "  +  
                fmt2.format(mPayment2) + "            " + fmt2.format(totAmount2)
                + "       " + fmt2.format(oPay2) + "          " 
                + fmt1.format(oPaypercent2) + "");

        //Prints third interest rate
        System.out.println("" + fmt1.format(aIR3) + "               "  +  
                fmt2.format(mPayment3) + "            " + fmt2.format(totAmount3)
                + "       " + fmt2.format(oPay3) + "          " 
                + fmt1.format(oPaypercent3) + "");

        //Prints fourth interest rate
        System.out.println("" + fmt1.format(aIR4) + "               "  +  
                fmt2.format(mPayment4) + "            " + fmt2.format(totAmount4)
                + "       " + fmt2.format(oPay4) + "          " 
                + fmt1.format(oPaypercent4) + "");

        //Prints fifth interest rate
        System.out.println("" + fmt1.format(aIR5) + "               "  +  
                fmt2.format(mPayment5) + "            " + fmt2.format(totAmount5)
                + "       " + fmt2.format(oPay5) + "          " 
                + fmt1.format(oPaypercent5) + "");
    }
输出

利率每月付款总额多付美元%多付
4%$1841.65$110499.13$10499.13 10.5%
4.5%$1864.30$111858.12$11858.12 11.9%
5%$1887.12$113227.40$13227.40 13.2%
5.5%$1910.12$114606.97$14606.97 14.6%

6%$1933.28$115996.81$15996.81 16%

应更改利率格式以处理十进制百分比值

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;

public class HelloWorld {

    public static void main(String[] args) {
        double aIR, mortgageAmount;
        int noY;
        double aIR1, aIR2, aIR3, aIR4, aIR5;
        double mIR1, mIR2, mIR3, mIR4, mIR5;
        double mPayment1, mPayment2, mPayment3, mPayment4, mPayment5;
        double totAmount1, totAmount2, totAmount3, totAmount4, totAmount5;
        double oPay1, oPay2, oPay3, oPay4, oPay5;
        double oPaypercent1, oPaypercent2, oPaypercent3, oPaypercent4, oPaypercent5;

        //This establishes formats for values
        NumberFormat fmt1 = new DecimalFormat("0.#%");
        //NumberFormat fmt1 = NumberFormat.getPercentInstance();
        NumberFormat fmt2 = NumberFormat.getCurrencyInstance();
        DecimalFormat fmt3 = new DecimalFormat("0.##");

        //This asks user for inputs
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter the Annual Interest Rate: ");
        aIR= scan.nextDouble();
        System.out.println("Enter the Number of Years you will Pay: ");
        noY = scan.nextInt();
        System.out.println("Enter Amount Borrowed from the Bank: ");
        mortgageAmount = scan.nextInt();

        //These will give annual interest rates depending on range
        aIR1 = (aIR - 1)/100;
        aIR2 = (aIR - 0.5)/100;
        aIR3 = (aIR)/100;
        aIR4 = (aIR + 0.5)/100;
        aIR5 = (aIR + 1)/100;

        //These give rates by month according to which rate being taken
        mIR1 = (aIR1 / 12);
        mIR2 = (aIR2 / 12);
        mIR3 = (aIR3 / 12);
        mIR4 = (aIR4 / 12);
        mIR5 = (aIR5 / 12);

        //This takes the amounts per month
        mPayment1 = (mIR1 * mortgageAmount)/(1-(1/Math.pow((1+mIR1),12*noY)));
        mPayment2 = (mIR2 * mortgageAmount)/(1-(1/Math.pow((1+mIR2),12*noY)));
        mPayment3 = (mIR3 * mortgageAmount)/(1-(1/Math.pow((1+mIR3),12*noY)));
        mPayment4 = (mIR4 * mortgageAmount)/(1-(1/Math.pow((1+mIR4),12*noY)));
        mPayment5 = (mIR5 * mortgageAmount)/(1-(1/Math.pow((1+mIR5),12*noY)));

        //This takes the total amount per year
        totAmount1 = mPayment1 * (noY*12);
        totAmount2 = mPayment2 * (noY*12);
        totAmount3 = mPayment3 * (noY*12);
        totAmount4 = mPayment4 * (noY*12);
        totAmount5 = mPayment5 * (noY*12);

        //This is the overpayment because of interest
        oPay1 = totAmount1 - (mortgageAmount);
        oPay2 = totAmount2 - (mortgageAmount);
        oPay3 = totAmount3 - (mortgageAmount);
        oPay4 = totAmount4 - (mortgageAmount);
        oPay5 = totAmount5 - (mortgageAmount);

        //This is the overpayment percentage
        oPaypercent1 = (oPay1/mortgageAmount);
        oPaypercent2 = (oPay2/mortgageAmount);
        oPaypercent3 = (oPay3/mortgageAmount);
        oPaypercent4 = (oPay4/mortgageAmount);
        oPaypercent5 = (oPay5/mortgageAmount);

        //Begins printing the results in a line
        System.out.println("The Mortgage Amount is: " 
                + fmt2.format(mortgageAmount));
        System.out.println("The Number of Years the Mortgage is Held: " + noY);
        System.out.println("Range of Interest Rates: " + fmt1.format(aIR1) 
                + " - " + fmt1.format(aIR5));
        System.out.println("Interest Rate   Monthly Payment   Total Payment"
                + "   $ Overpayment   % Overpayment");

        //Prints first interest rate
        System.out.println("" + fmt1.format(aIR1) + "               "  +  
                fmt2.format(mPayment1) + "            " + fmt2.format(totAmount1)
                + "       " + fmt2.format(oPay1) + "          " 
                + fmt1.format(oPaypercent1) + "");

        //Prints second interest rate
        System.out.println("" + fmt1.format(aIR2) + "               "  +  
                fmt2.format(mPayment2) + "            " + fmt2.format(totAmount2)
                + "       " + fmt2.format(oPay2) + "          " 
                + fmt1.format(oPaypercent2) + "");

        //Prints third interest rate
        System.out.println("" + fmt1.format(aIR3) + "               "  +  
                fmt2.format(mPayment3) + "            " + fmt2.format(totAmount3)
                + "       " + fmt2.format(oPay3) + "          " 
                + fmt1.format(oPaypercent3) + "");

        //Prints fourth interest rate
        System.out.println("" + fmt1.format(aIR4) + "               "  +  
                fmt2.format(mPayment4) + "            " + fmt2.format(totAmount4)
                + "       " + fmt2.format(oPay4) + "          " 
                + fmt1.format(oPaypercent4) + "");

        //Prints fifth interest rate
        System.out.println("" + fmt1.format(aIR5) + "               "  +  
                fmt2.format(mPayment5) + "            " + fmt2.format(totAmount5)
                + "       " + fmt2.format(oPay5) + "          " 
                + fmt1.format(oPaypercent5) + "");
    }
输出

利率每月付款总额多付美元%多付
4%$1841.65$110499.13$10499.13 10.5%
4.5%$1864.30$111858.12$11858.12 11.9%
5%$1887.12$113227.40$13227.40 13.2%
5.5%$1910.12$114606.97$14606.97 14.6%

6%$1933.28$115996.81$15996.81 16%

应更改利率格式以处理小数百分比值。应更改利率格式以处理小数百分比值。