0

I am currently trying to make a program that will estimate a phone bill. If you input "r", you are charged a weekly rate of $5 plus 10 cents per minute for each minute over 60. However, whenever I input "80", the program replies with "You owe 6.999999999999993! Instead of "You owe 7!" I am very confused onto why it returns this large decimal instead of 7.

import java.util.*;
public class MobilePhoneCharges 
{

    public static void main(String[] args) 
    {
        double money = 0;
        String userInput;
        Scanner sc = new Scanner(System.in);
        System.out.print("Hello! Welcome to MOBILEPHONECOMPANY! \nPlease enter the type of customer you are! ");
        userInput = sc.nextLine();

        if(userInput.equals("r") || userInput.equals("c") || userInput.equals("p") ) {
            System.out.println("Please enter in how many minutes you have used! ");
            int minutes = sc.nextInt();

            if (userInput.equals("r") && minutes>0) {
                money += 5;
                if(minutes >= 60) {
                    minutes = minutes - 60;
                    for(;minutes!=0; minutes--) {
                        money = money + .10;
                    }
                }
                System.out.println("You owe "+money+"!");
            }

        }
    }
}

If I replace

money = money + .10;

with

money = money + .50;

it will work fine and return "You owe 15.0!" with no large decimal. Because of this, I am very confused onto why .10 leads to such a large decimal.

  • 1
    "*I am currently trying to make a program that will estimate a phone bill.*" - Never use `float` or `double` for anything remotely related to finance or money. – Turing85 Nov 05 '22 at 23:40
  • You might want to look at https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Progman Nov 06 '22 at 00:35

0 Answers0