Advertisement
milen8204

[C# Basics]ConsoleInputOut Problem 5. Formatting Numbers

Mar 19th, 2014
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.17 KB | None | 0 0
  1. using System;
  2. using System.Globalization;
  3. using System.Threading;
  4. /* Write a program that reads 3 numbers: an integer a (0 ≤ a ≤ 500),
  5.  * a floating-point b and a floating-point c and prints them in 4 virtual columns on the console. Each column should have a width of 10 characters.
  6.  * The number a should be printed in hexadecimal, left aligned; then the number a should be printed in binary form, padded with zeroes,
  7.  * then the number b should be printed with 2 digits after the decimal point,
  8.  * right aligned; the number c should be printed with 3 digits after the decimal point, left aligned.
  9.  * Examples:
  10.  * a        b       c       result
  11.  * 254      11.6    0.5     |FE        |0011111110|     11.60|0.500     |
  12.  * 499      -0.5559 10000   |1F3       |0111110011|     -0.56|10000     |
  13.  * 0        3       -0.1234 |0         |0000000000|         3|-0.123    | */
  14.  
  15. class FormattingNumbers
  16. {
  17.     static void Main()
  18.     {
  19.         Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;
  20.         int numberA = 0;
  21.         double numberB = 0;
  22.         double numberC = 0;
  23.         do
  24.         {
  25.             Console.WriteLine("Please, enter an integer a (0 ≤ a ≤ 500), a floating-point b, a floating-point c," +
  26.                                 " by hitting \"Еnter\" for each:");
  27.             bool isParseSuccessful = int.TryParse(Console.ReadLine(), out numberA) && numberA >= 0 && numberA <= 500 &&
  28.                                      double.TryParse(Console.ReadLine(), out numberB) &&
  29.                                      double.TryParse(Console.ReadLine(), out numberC);            
  30.             if (isParseSuccessful)
  31.             {
  32.                 break;
  33.             }
  34.             else
  35.             {
  36.                 Console.WriteLine("You have entered an invalid data. Try again or press \"Cntrl + C\"!");
  37.             }
  38.  
  39.         } while (true);
  40.  
  41.         Console.Clear();
  42.         Console.WriteLine("{0,-10}{1,-10}{2,-10}{3}", "a", "b","c","result");
  43.         Console.WriteLine("{0,-10}{1,-10}{2,-10}|{3,-10:X}|{4,-10}|{5,10:#.##}|{6,-10:0.000}|",
  44.             numberA, numberB, numberC, numberA, Convert.ToString(numberA, 2).PadLeft(10, '0'), numberB, numberC);
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement