mark

mark

  • NA
  • 26
  • 0

decimal point problem

Aug 8 2007 1:21 PM

Hi. I'm just creating a very small "cash register". When I display the total to the user, the decimal goes about 5 places. I only need it to go two places, obviously. How might I go about doing this?

 

[code]

static void Main(string[] args)
        {
            double itemPrice = 0.00f;
            double totalAmount = 0.0f;
            double taxAmount = 0.00f;

            string newItem = "n";
            bool finished = false;


            while (!finished)
            {
                Console.Write("Enter item amount: ");
                itemPrice = Convert.ToDouble(Console.ReadLine());

                Console.Write("Add another item? (Y / N): ");
                newItem = Console.ReadLine();
                totalAmount = totalAmount + itemPrice;

                if (newItem == "n" || newItem == "N")//If no more items need to be added, display total.
                {
                    finished = true;
                }
                else if(newItem == "y" || newItem == "Y")//If more items need to be added, ask price.
                {
                    Console.WriteLine("Okay, let's add another item to the cart.");
                }
                else
                {
                    Console.WriteLine("Thats and incorrect command. You are done!");
                    finished = true;
                }
               
            }

            Console.WriteLine("{0} is your total before tax.", totalAmount);

            Console.Write("Adding tax...");
            taxAmount = totalAmount * 0.525;
            totalAmount = totalAmount + taxAmount;

            Console.Write("Your complete total comes to {0}.", totalAmount);
            Console.ReadKey();  //Wait for user to hit a key before exiting!
        }

[/code]


Answers (4)