C# 練習題 (7)

C# 練習題 (7)

練習題 (7):Printing output in different formats (say rounding up to 5 decimal places, truncating after 4 decimal places, padding zeros to the right and left, right and left justification)(Input output operations)

這一題主要是練習字串的 Format,四捨五入的捨去、無條件捨去、數值向左補零、數值向右補零、字串向左對齊、向右對齊。

程式碼:

  1. using System;
  2. using System.Text;
  3. namespace Exercise07
  4. {
  5. internal class Program
  6. {
  7. private static void Main( string [ ] args)
  8. {
  9. double d = 12345.456789;
  10. Console.WriteLine ( "{0:F5}", d);
  11. d = Math.Floor (d*(Math.Pow ( 10.0, 4.0 ) ) )/Math.Pow ( 10.0, 4.0 );
  12. Console.WriteLine ( "{0}", d);
  13. Console.WriteLine ( "{0:0000000000.####}", d);
  14. Console.WriteLine ( "{0:.0000000000}", d);
  15. StringBuilder sb = new StringBuilder( "Eric" );
  16. Console.WriteLine ( "{0,10}", sb);
  17. Console.WriteLine ( "{0,-10}", sb);
  18. }
  19. }
  20. }