Advertisement
Mitax

Formatting_Numbers

Jun 12th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. /*
  2. Write a program that reads 3 numbers (separated by whitespace): an integer a (0 ≤ a ≤ 2222),
  3. a floating-point b and a floating-point c and prints them in 4 virtual columns on the console.
  4. Each column should have a width of 10 characters. The number a should be printed in hexadecimal,
  5. left aligned; then the number a should be printed in binary form, padded with zeroes (if it is
  6. bigger than 10 bits remove the least significant ones), then the number b should be printed with
  7. 2 digits after the decimal point, right aligned; the number c should be printed with 3 digits
  8. after the decimal point, left aligned.
  9. */
  10.  
  11. using System;
  12. using System.Globalization;
  13. using System.Threading;
  14.  
  15. namespace _3.Formatting_Numbers
  16. {
  17. class FormatingNum
  18. {
  19. static void Main(string[] args)
  20. {
  21. Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB");
  22. var inputNumbers = Console.ReadLine().Split(new[] { ' ', '\t'}, StringSplitOptions.RemoveEmptyEntries);
  23. var numberA = int.Parse(inputNumbers[0]);
  24. var numberB = double.Parse(inputNumbers[1]);
  25. var numberC = double.Parse(inputNumbers[2]);
  26.  
  27. var numberAinHEX = $"{numberA:X}";
  28. var formatedNumberB = $"{numberB:f2}";
  29. var formatedNumberC = $"{numberC:f3}";
  30.  
  31. Console.Write("|{0}", numberAinHEX.PadRight(10, ' '));
  32. Console.Write("|{0}", Convert.ToString(numberA, 2).PadLeft(10, '0'));
  33. Console.Write("|{0}", formatedNumberB.PadLeft(10, ' '));
  34. Console.WriteLine("|{0}", formatedNumberC.PadRight(10, ' ') + "|");
  35. }
  36. }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement