2013-02-10 54 views
0

我是Java編程新手。我有一項計劃未來投資價值的計劃。我已經編寫的程序,它的工作原理,但我的教師要求顯示用戶的輸入,我無法在我的文本或在線查找有關此主題的任何信息。我已經發郵件給我的導師沒有結果。下面你會看到該程序。我也無法找到如何舍入到最小的小數點。 謝謝任何​​能夠提供幫助的人。如何顯示用戶輸入的值並四捨五入到最接近的小數點?

import javax.swing.JOptionPane; 

/* This program will calculate the future investment value after entering the 
* investment amount, annual interest rate and the number of years for the investment*/ 


public class FutureInvestment { 
    public static void main(String[] args){ 

    //Enter annual interest rate 
    String annualInterestRateString = JOptionPane.showInputDialog(
    "Enter annual interest rate, for example, 8.25:"); 

    //Convert string to double 
    double annualInterestRate = Double.parseDouble(annualInterestRateString); 

    //Obtain monthly interest rate 
    double monthlyInterestRate = annualInterestRate/1200; 

    //Enter number of years 
    String numberOfYearsString = JOptionPane.showInputDialog(
    "Enter number of years as an integer, for example, 5:"); 

    //Convert string to integer 
    int numberOfYears = Integer.parseInt(numberOfYearsString); 

    //Enter investment amount 
    String investmentAmountString = JOptionPane.showInputDialog(
    "Enter investment amount, for example, 120000.95:"); 

    //Convert string to double 
    double investmentAmount = Double.parseDouble(investmentAmountString); 

    //Calculate future investment amount 
    double futureInvestmentAmount = investmentAmount * (Math.pow(1 + monthlyInterestRate, 
numberOfYears * 12)); 

    //Format to keep 2 digits after the decimal point 
    futureInvestmentAmount = (int)(futureInvestmentAmount * 100)/100.0; 

    //Display results 
    String output = "The Future Investment Value is $" + futureInvestmentAmount; 

    JOptionPane.showMessageDialog(null, output); 

    } 

} 

回答

0

您可以使用String。格式來格式化包含具有特定精度的數字的消息。例如,這輪量到小數點後兩位:

String output =String.format("The Future Investment Value is $%.2f", futureInvestmentAmount) 
0

爲了幫助舍入和精度,我推薦使用的BigDecimal代替double。由於floating point arithmetic imprecision

final String annualInterestRateString = JOptionPane.showInputDialog("Enter annual interest rate, for example, 8.25:"); 
final BigDecimal annualInterestRate = new BigDecimal(annualInterestRateString); 
final BigDecimal monthlyInterestRate = annualInterestRate.divide(new BigDecimal("1200"), RoundingMode.HALF_EVEN); 
final String numberOfYearsString = JOptionPane.showInputDialog("Enter number of years as an integer, for example, 5:"); 
final Integer numberOfYears = Integer.parseInt(numberOfYearsString); 
final String investmentAmountString = JOptionPane.showInputDialog("Enter investment amount, for example, 120000.95:"); 
final BigDecimal investmentAmount = new BigDecimal(investmentAmountString); 
final BigDecimal futureInvestmentAmount = investmentAmount.multiply(monthlyInterestRate.add(BigDecimal.ONE).pow(numberOfYears * 12), new MathContext(2, RoundingMode.HALF_EVEN)); 
final String output = "The Future Investment Value is $" + new DecimalFormat("#,##0.00;-#,##0.00").format(futureInvestmentAmount); 
JOptionPane.showMessageDialog(null, output);