1 Replies - 1421 Views - Last Post: 01 May 2009 - 01:26 AM Rate Topic: -----

#1 Tastybrownies  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 51
  • Joined: 13-May 08

Certain Formatting Questions for output in console

Post icon  Posted 30 April 2009 - 11:56 PM

Hello everyone, after hours of trying to get my program to have the correct formatting, I figured I'd come here and just ask. Go figure, I am pretty much done with the program excpet this one thing is hanging me up right now.

To keep it short, my class just started doing arrays and this was one of our homework assignments. So I made up my array, console input, constants, and everything was working fine and dandy, but like I said there's that one format issue toward the bottom.

This is the program code:

If I input 262 for the area code and 7 for the minutes I want it to somehow output $0.49, but if I try to put anything such as ("C0") or this ("C2") it just either makes the output zeros or rounds it to 0.50!

using System;

	class ChatAWhile
	{
		 static void Main()
		{
			// declaring the variables for the program.

			
			double[] areaCode;
			areaCode = new double[6];
			const double TWO_SIXTY_TWO = 0.07, FOUR_FOURTEEN = 0.10, SIX_HUNDRED_EIGHT = 0.05, SEVEN_FIFTEEN = 0.16, EIGHT_FIFTEEN = 0.24, NINE_TWENTY = 0.14;
			double minutesUserInput;
			int areaCodeUserInput;
			double totalCost = 0; 
			int x;
			
			areaCode[0] = 262;
			areaCode[1] = 414;
			areaCode[2] = 608;
			areaCode[3] = 715;
			areaCode[4] = 815;
			areaCode[5] = 920;

			Console.WriteLine("Chat-A-While Telephone Company");
			Console.WriteLine();
			Console.Write("Enter the area code: ");
			// areaCodeAsString = Console.ReadLine();
			areaCodeUserInput = Convert.ToInt32(Console.ReadLine());
			Console.Write("Enter the calling time(minutes): ");
			minutesUserInput = Convert.ToDouble(Console.ReadLine());
			Console.WriteLine();

			
			// using a switch statement to match user minutes with area code.
			 switch(areaCodeUserInput)
			 {
				 case 262:
					 totalCost = TWO_SIXTY_TWO * minutesUserInput;
					 break;
				 case 414:
					 totalCost = FOUR_FOURTEEN * minutesUserInput;
					 break;
				 case 608:
					 totalCost = SIX_HUNDRED_EIGHT * minutesUserInput;
					 break;
				 case 715:
					 totalCost = SEVEN_FIFTEEN * minutesUserInput;
					 break;
				 case 815:
					 totalCost = EIGHT_FIFTEEN * minutesUserInput;
					 break;
				 case 920:
					 totalCost = NINE_TWENTY * minutesUserInput;
					 break;

			 }
				// Was trying to explore different options here............
			 // string s = String.Format("{0:0.00}", totalCost);
			x = Array.BinarySearch(areaCode, areaCodeUserInput);
			if (x < 0)
				Console.WriteLine("Our company does not cover the area code {0}. Please enter a valid area code.",
					areaCodeUserInput);
			else
				Console.WriteLine("The cost of a phone call to area code {0} is: " + totalCost.ToString("C") +
					x, areaCode[x].ToString());

			Console.Read();
		}
		
	}



Is This A Good Question/Topic? 0
  • +

Replies To: Certain Formatting Questions for output in console

#2 ragingben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 170
  • View blog
  • Posts: 637
  • Joined: 07-October 08

Re: Certain Formatting Questions for output in console

Posted 01 May 2009 - 01:26 AM

Hi, you could try...
Math.Round(totalCost,2);




Which would round to 2 decimal places and stop the formatting?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1