Csharp/C Sharp/Development Class/Number Format — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 11:45, 26 мая 2010
Содержание
- 1 A closer look at Format()
- 2 Format an enumeration
- 3 Illustrates formatting numbers
- 4 Numeric Formatting:Custom Format Strings:Decimal Point
- 5 Numeric Formatting:Custom Format Strings:Digit or Space Placeholder
- 6 Numeric Formatting:Custom Format Strings:Digit or Zero Placeholder
- 7 Numeric Formatting:Custom Format Strings:Escapes and Literals
- 8 Numeric Formatting:Custom Format Strings:Exponential Notation
- 9 Numeric Formatting:Custom Format Strings:Group Separator
- 10 Numeric Formatting:Custom Format Strings:Number Prescaler
- 11 Numeric Formatting:Custom Format Strings:Percent Notation
- 12 Numeric Formatting:Custom Format Strings:Section Separator
- 13 Numeric Formatting:Standard Format Strings:Currency
- 14 Numeric Formatting:Standard Format Strings:Decimal
- 15 Numeric Formatting:Standard Format Strings:Fixed-Point
- 16 Numeric Formatting:Standard Format Strings:General
- 17 Numeric Formatting:Standard Format Strings:Hexadecimal
- 18 Numeric Formatting:Standard Format Strings:Number
- 19 Numeric Formatting:Standard Format Strings:Scientific (Exponential)
- 20 Use String.Format() to format a value
- 21 Use ToString() to format values
- 22 Using custom formats
A closer look at Format()
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A closer look at Format().
using System;
public class FormatDemo2 {
public static void Main() {
int i;
int sum = 0;
int prod = 1;
string str;
/* Display the running sum and product
for the numbers 1 through 10. */
for(i=1; i <= 10; i++) {
sum += i;
prod *= i;
str = String.Format("Sum:{0,3:D} Product:{1,8:D}",
sum, prod);
Console.WriteLine(str);
}
}
}
Format an enumeration
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Format an enumeration.
using System;
public class EnumFmtDemo {
enum Direction { North, South, East, West }
[Flags] enum Status { Ready=0x1, OffLine=0x2,
Waiting=0x4, TransmitOK=0x8,
RecieveOK=0x10, OnLine=0x20 }
public static void Main() {
Direction d = Direction.West;
Console.WriteLine("{0:G}", d);
Console.WriteLine("{0:F}", d);
Console.WriteLine("{0:D}", d);
Console.WriteLine("{0:X}", d);
Status s = Status.Ready | Status.TransmitOK;
Console.WriteLine("{0:G}", s);
Console.WriteLine("{0:F}", s);
Console.WriteLine("{0:D}", s);
Console.WriteLine("{0:X}", s);
}
}
Illustrates formatting numbers
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example2_15.cs illustrates formatting numbers
*/
public class Example2_151
{
public static void Main()
{
// formatting integers
int myInt = 12345;
int myInt2 = 67890;
System.Console.WriteLine("myInt = {0, 6}, myInt2 = {1, 5}",
myInt, myInt2);
System.Console.WriteLine("myInt using 10:d = {0, 10:d}",
myInt);
System.Console.WriteLine("myInt using 10:x = {0, 10:x2}",
myInt);
// formatting floating-point numbers
double myDouble = 1234.56789;
System.Console.WriteLine("myDouble using 10:f3 = {0, 10:f3}",
myDouble);
float myFloat = 1234.56789f;
System.Console.WriteLine("myFloat using 10:f3 = {0, 10:f3}",
myFloat);
decimal myDecimal = 1234.56789m;
System.Console.WriteLine("myDecimal using 10:f3 = {0, 10:f3}",
myDecimal);
System.Console.WriteLine("myFloat using 10:e3 = {0, 10:e3}",
myFloat);
System.Console.WriteLine("myFloat using 10:p2 = {0, 10:p2}",
myFloat);
System.Console.WriteLine("myFloat using 10:n2 = {0, 10:n2}",
myFloat);
System.Console.WriteLine("myFloat using 10:g2 = {0, 10:g2}",
myFloat);
// formatting currency values
decimal myMoney = 15123.45m;
System.Console.WriteLine("myMoney using 10:c2 = {0, 10:c2}",
myMoney);
}
}
Numeric Formatting:Custom Format Strings:Decimal Point
using System;
public class DecimalPoint
{
public static void Main()
{
Console.WriteLine("{0:#####.000}", 75928.3);
Console.WriteLine("{0:##.000}", 1456.456456);
}
}
Numeric Formatting:Custom Format Strings:Digit or Space Placeholder
using System;
public class DigitorSpacePlaceholder
{
public static void Main()
{
Console.WriteLine("{0:#####}", 255);
Console.WriteLine("{0:#####}", 1456);
Console.WriteLine("{0:###}", 32767);
}
}
Numeric Formatting:Custom Format Strings:Digit or Zero Placeholder
using System;
public class DigitorZeroPlaceholder
{
public static void Main()
{
Console.WriteLine("{0:000}", 55);
Console.WriteLine("{0:000}", 1456);
}
}
Numeric Formatting:Custom Format Strings:Escapes and Literals
using System;
public class EscapesandLiterals
{
public static void Main()
{
Console.WriteLine("{0:###\\#}", 255);
Console.WriteLine(@"{0:###\#}", 255);
Console.WriteLine("{0:###"#0%;"}", 1456);
}
}
Numeric Formatting:Custom Format Strings:Exponential Notation
using System;
public class ExponentialNotation
{
public static void Main()
{
Console.WriteLine("{0:###.000E-00}", 3.1415533E+04);
Console.WriteLine("{0:#.0000000E+000}", 2.553939939E+101);
}
}
Numeric Formatting:Custom Format Strings:Group Separator
using System;
public class GroupSeparator
{
public static void Main()
{
Console.WriteLine("{0:##,###}", 2555634323);
Console.WriteLine("{0:##,000.000}", 14563553.593993);
Console.WriteLine("{0:#,#.000}", 14563553.593993);
}
}
Numeric Formatting:Custom Format Strings:Number Prescaler
using System;
public class NumberPrescaler
{
public static void Main()
{
Console.WriteLine("{0:000,.##}", 158847);
Console.WriteLine("{0:000,,,.###}", 1593833);
}
}
Numeric Formatting:Custom Format Strings:Percent Notation
using System;
public class PercentNotation
{
public static void Main()
{
Console.WriteLine("{0:##.000%}", 0.89144);
Console.WriteLine("{0:00%}", 0.01285);
}
}
Numeric Formatting:Custom Format Strings:Section Separator
using System;
public class SectionSeparator
{
public static void Main()
{
Console.WriteLine("{0:###.00;0;(###.00)}", -456.55);
Console.WriteLine("{0:###.00;0;(###.00)}", 0);
Console.WriteLine("{0:###.00;0;(###.00)}", 456.55);
}
}
Numeric Formatting:Standard Format Strings:Currency
using System;
public class Currency
{
public static void Main()
{
Console.WriteLine("{0:C}", 33345.8977);
Console.WriteLine("{0:C}", -33345.8977);
}
}
Numeric Formatting:Standard Format Strings:Decimal
using System;
public class StandardFormatStringsDecimal {
public static void Main()
{
Console.WriteLine("{0:D}", 33345);
Console.WriteLine("{0:D7}", 33345);
}
}
Numeric Formatting:Standard Format Strings:Fixed-Point
using System;
public class FixedPoint {
public static void Main()
{
Console.WriteLine("{0:F}", 33345.8977);
Console.WriteLine("{0:F0}", 33345.8977);
Console.WriteLine("{0:F5}", 33345.8977);
}
}
Numeric Formatting:Standard Format Strings:General
using System;
public class StandardFormatStringsGeneral
{
public static void Main()
{
Console.WriteLine("{0:G}", 33345.8977);
Console.WriteLine("{0:G7}", 33345.8977);
Console.WriteLine("{0:G4}", 33345.8977);
}
}
Numeric Formatting:Standard Format Strings:Hexadecimal
using System;
public class Hexadecimal
{
public static void Main()
{
Console.WriteLine("{0:X}", 255);
Console.WriteLine("{0:x8}", 1456);
}
}
Numeric Formatting:Standard Format Strings:Number
using System;
public class StandardFormatStringsNumber
{
public static void Main()
{
Console.WriteLine("{0:N}", 33345.8977);
Console.WriteLine("{0:N4}", 33345.8977);
}
}
Numeric Formatting:Standard Format Strings:Scientific (Exponential)
using System;
public class ScientificExponential
{
public static void Main()
{
Console.WriteLine("{0:E}", 33345.8977);
Console.WriteLine("{0:E10}", 33345.8977);
Console.WriteLine("{0:e4}", 33345.8977);
}
}
Use String.Format() to format a value
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use String.Format() to format a value.
using System;
public class FormatDemo1 {
public static void Main() {
double v = 17688.65849;
double v2 = 0.15;
int x = 21;
string str = String.Format("{0:F2}", v);
Console.WriteLine(str);
str = String.Format("{0:N5}", v);
Console.WriteLine(str);
str = String.Format("{0:e}", v);
Console.WriteLine(str);
str = String.Format("{0:r}", v);
Console.WriteLine(str);
str = String.Format("{0:p}", v2);
Console.WriteLine(str);
str = String.Format("{0:X}", x);
Console.WriteLine(str);
str = String.Format("{0:D12}", x);
Console.WriteLine(str);
str = String.Format("{0:C}", 189.99);
Console.WriteLine(str);
}
}
Use ToString() to format values
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use ToString() to format values.
using System;
public class ToStringDemo {
public static void Main() {
double v = 17688.65849;
double v2 = 0.15;
int x = 21;
string str = v.ToString("F2");
Console.WriteLine(str);
str = v.ToString("N5");
Console.WriteLine(str);
str = v.ToString("e");
Console.WriteLine(str);
str = v.ToString("r");
Console.WriteLine(str);
str = v2.ToString("p");
Console.WriteLine(str);
str = x.ToString("X");
Console.WriteLine(str);
str = x.ToString("D12");
Console.WriteLine(str);
str = 189.99.ToString("C");
Console.WriteLine(str);
}
}
Using custom formats
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using custom formats.
using System;
public class PictureFormatDemo {
public static void Main() {
double num = 64354.2345;
Console.WriteLine("Default format: " + num);
// Display with 2 decimal places.
Console.WriteLine("Value with two decimal places: " +
"{0:#.##}", num);
// Display with commas and 2 decimal places.
Console.WriteLine("Add commas: {0:#,###.##}", num);
// Display using scientific notation.
Console.WriteLine("Use scientific notation: " +
"{0:#.###e+00}", num);
// Scale the value by 1000.
Console.WriteLine("Value in 1,000s: " +
"{0:#0,}", num);
/* Display positive, negative, and zero
values differently. */
Console.WriteLine("Display positive, negative, " +
"and zero values differently.");
Console.WriteLine("{0:#.#;(#.##);0.00}", num);
num = -num;
Console.WriteLine("{0:#.##;(#.##);0.00}", num);
num = 0.0;
Console.WriteLine("{0:#.##;(#.##);0.00}", num);
// Display a percentage.
num = 0.17;
Console.WriteLine("Display a pecentage: {0:#%}", num);
}
}