. . Hong Kong Business Software Company
Expert in web-based solutions
Software House in Hong Kong

Microsoft Certified Solution Developer

 

Chief Architect's Blog on software development

7 Oct 2008
.Net Number Formatting
In computing, numeric data are stored as number datatype.  Number datatypes are useful for mathematic calculation but raw number does not format nicely for readability.  If we want to display number to user in special format, we may need to do something extra.  For example, we may want an integer  of 1234 to be displayed as 1,234.00 or 001234 or HK$1,234.

All datatype of Microsoft .Net platform has a ToString() method. Numeric datatype's ToString() method further allows you to apply various style to the number before converting it to string.  The following table shows some numeric values converted to string with various styles using ToString() method.
Value of myNumber ToString() Method Output
123 myNumber.ToString("00000") 00123
1234567 myNumber.ToString("#,#") 1,234,567
123.4 nyNumber.ToString("#.#0") 123.40
1234 myNumber.ToString("##;(##)") 1234
-1234 myNumber.ToString("##;(##)") (1234)
12345 myNumber.ToString("HK$#,#.#0") HK$12,345.00
0.012 myNumber.ToSting("#0.##%") 1.2%
85222334455 myNumber.ToSting("(###)####-####") (852)2233-4455
In fact, there are some other combinations. You can do some tests to find out the actual output.

The following is a simple demo of the above style.  You can change the number and select a style to see what the output looks like.