練習題 (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,四捨五入的捨去、無條件捨去、數值向左補零、數值向右補零、字串向左對齊、向右對齊。
程式碼:
-
using System;
-
using System.Text;
-
namespace Exercise07
-
{
-
internal class Program
-
{
-
private static void Main( string [ ] args)
-
{
-
double d = 12345.456789;
-
Console.WriteLine ( "{0:F5}", d);
-
d = Math.Floor (d*(Math.Pow ( 10.0, 4.0 ) ) )/Math.Pow ( 10.0, 4.0 );
-
Console.WriteLine ( "{0}", d);
-
Console.WriteLine ( "{0:0000000000.####}", d);
-
Console.WriteLine ( "{0:.0000000000}", d);
-
StringBuilder sb = new StringBuilder ( "Eric" );
-
Console.WriteLine ( "{0,10}", sb);
-
Console.WriteLine ( "{0,-10}", sb);
-
}
-
}
-
}
|