0% found this document useful (0 votes)
5 views1 page

Complete-Reference-Vb Net 28

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views1 page

Complete-Reference-Vb Net 28

Uploaded by

khalid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Custom Formatters

The following code writes the following Strings to the console:

123456.700
12345.670
1.235

Using the Group Separator

The group separator is a comma (,) and can be used to format large numbers to make them easier to read. You
typically add the comma three places after the decimal point to specify a number such as 1,000.00 or higher.
The character used as the specifier can also be customized in the NumberFormatInfo class. The following
example illustrates placement of the group separator:

Console.WriteLine("{0:##,###}", 123456.7)
Console.WriteLine("{0:##,###,000.000}", 1234567.1234567)
Console.WriteLine("{0:#,#.000}", 1234567.1234567)

The output to console looks like this:

123,457
1,234,567.123
1,234,567.123

Using Percent Notation

You can use the percent (%) specifier to denote that a number be displayed as a percentage. The number will
be multiplied by 100 before formatting. In the following example:

Console.WriteLine("{0:##,000%}", 123.45)
Console.WriteLine("{0:00%}", 0.123)

you get the following percentages displayed in the console:

12,345%
12%

Building Strings with StringBuilder


The efficiency of the String object as an immutable type has its downside. Every time you change the String,
you create a new String object that requires its own memory location. If you need to repetitively work with a
String, shaping it for a particular task, you have the additional overhead of the constant creation of new
String objects every time you need to cut, add, and move characters around in the String.

When you need to constantly work with a String, such as an algorithm that takes UNC paths and converts
them to HTML paths, or when you need a storage location to shove characters into, like a stack, then you need
to turn to the StringBuilder class. This class can be found on the System.Text.StringBuilder namespace and
allows you to keep working with a String of characters represented by the same objects for as long as it is
needed. The great feature of the object is that you get to reference the collection of characters as a single
Stringfar less code than that "soda−fountain" Stack that requires extensive "popping."

Note In the BitShifters code in Chapter 5, we used the StringBuilder object, albeit in C# garb, as a place to
stuff bits.

512

You might also like