简体   繁体   中英

How to display values entered in by user and round to the nearest decimal point?

I am new to Java programing. I have an assignment for a program to calculate future investment value. The program I have already wrote and it works but my instructor is asking to display the user's input and I am unable to find any information about this topic either in my text or online. I have emailed my instructor with no results. Below you will see the program. I am also not able to find how to round to the neast decimal point. Thank you to anyone that can help.

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);

   }

}

You can use String. format to format messages that include numbers with a specific precision. For example this rounds the amount to two decimal places:

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

To help with rounding and precision, I'd recommend using BigDecimal instead of double . Due to 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM