Characters Used for Numeric Formatting - CSharp Language Basics

CSharp examples for Language Basics:Data Type

Introduction

Specifier
C or c

Description
Currency

Default Format
$xx,xxx.xx
($xx,xxx.xx)
Example Output
$12,345.67
($12,345.67)
D or d

Decimal

xxxxxxx
-xxxxxxx
1234567
-1234567
E or e







Exponential







x.xxxxxxE+xxx
x.xxxxxxe+xxx
-x.xxxxxxxE+xxx
-x.xxxxxxxe+xxx
x.xxxxxxE-xxx
x.xxxxxxe-xxx
-x.xxxxxxxE-xxx
-x.xxxxxxxe-xxx
1.234567E+123
1.234567e+123
-1.234567E+123
-1.234567e+123
1.234567E-123
1.234567e-123
-1.234567E-123
-1.234567e-123
F or f

Fixed point

xxxxxx.xx
-xxxxx.xx
1234567.89
-1234567.89
N or n

Numeric

xx,xxx.xx
-xx,xxx.xx
12,345.67
-12,345.67
X or x
Hexadecimal
12d687
12D687
G or g
General
Varies (uses the most compact format)
R or r
Round-trip
Maintains precession when numbers are converted to and then back from a string

Using the General format specifier

Demo Code

using System;//w w w .ja  v a 2s .  com
class General
{
   public static void Main()
   {
      float fVal1 = .000000789F;
      float fVal2 = 1.2F;
      Console.WriteLine("f1   ({0:f}). Format (G): {0:G}", fVal1);
      Console.WriteLine("f2   ({0:f}). Format (G): {0:G}", fVal2);
   }
}

Result


Related Tutorials