-1

Prompt: It is difficult to make a budget that spans several years, because prices are not stable. If your company needs 200 pencils per year, you cannot simply use this years price as the cost of pencils two years from now. Because of inflation, the cost is likely to be higher than it is today. Write a program to gauge the expected cost of an item in a specified number of years. The program asks for the cost of the item, the number of years from now that the item will be purchased, and the rate of inflation. The program then outputs the estimated cost of the item after the specified period. Have the user enter the inflation rate as a percentage, such as 5.6 (percent). Your program should then convert the percent to a fraction, such as 0.056 and should use a loop to estimate the price adjusted for inflation.

Code:

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

    public class InflationCalculator {

        public static void main(String[] args) {
     
           Scanner console = new Scanner(System.in);
   
           System.out.print("Enter price of the Item:");
           double cost = console.nextDouble();
    
           System.out.print("Enter number of years in which it will be purchased:");
           double years = console.nextDouble();
    
           System.out.print("Enter percent of inflation per year:");
           double inflationRate = console.nextDouble();       
     
           inflationRate = inflationRate / 100;
   
           for(int i = 1; i <= years; i++){
           cost += cost * inflationRate;
        }
   
      System.out.println(cost);
     }   
 }

Expected Result:

Enter·price·of·the·Item:200↵ Enter·number·of·years·in·which·it·will·be·purchased:50↵ Enter·percent·of·inflation·per·year:5↵ 2293.4799571507338

What I got :

Enter·price·of·the·Item:200↵ Enter·number·of·years·in·which·it·will·be·purchased:50↵ Enter·percent·of·inflation·per·year:5↵ 2293.479957150734

I'm not sure why it is not rounding correctly, what did I do wrong ???

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Sofia Varner
  • 39
  • 2
  • 7

2 Answers2

2

This code should work well:

    Scanner console = new Scanner(System.in);

    System.out.print("Enter price of the Item:");
    BigDecimal cost = new BigDecimal(console.nextDouble());

    System.out.print("Enter number of years in which it will be purchased:");
    int years = console.nextInt();

    System.out.print("Enter percent of inflation per year:");
    BigDecimal inflationRate = new BigDecimal(console.nextDouble());

    inflationRate = inflationRate.divide(new BigDecimal(100));
    
    for(int i = 1; i <= years; i++) {
        cost = cost.add(cost.multiply(inflationRate));
    }

    System.out.println(cost);

The output (using your inputs): 2293.4799571507352069702827102422103183593712979003728316020223783073817003241856582462787628173828125000

If you want to round this number to your expected result, you can use

System.out.println(cost.setScale(10, BigDecimal.ROUND_CEILING)); // to round up
// or
System.out.println(cost.setScale(10, BigDecimal.ROUND_FLOOR)); // to round down

The first parameter of .setScale() is the number of decimal digit that you want to show up. (sorry for my bad english lol)

Crih.exe
  • 502
  • 4
  • 16
1

Problem:

The double variable type has a precision of about 15-16 decimal points. This is why your result is being truncated.

Solution:

You can instead use BigDecimal or DecimalFormat, as explained in these answers.

bordeaux
  • 333
  • 3
  • 15