12345678, 2.12345678)
Console.WriteLine("US format: {0}", num1.ToString("F5", _
New CultureInfo("en-US")))
Console.WriteLine("DE format: {0}", num1.ToString("F5", _
New CultureInfo("de-DE")))
Console.WriteLine("Object.ToString(): {0}", num1.ToString())
End Sub
End Class
Here??™s the output from running the previous example:
US format: (1.12346 1.12346)
DE format: (1,12346 1,12346)
Object.ToString(): (1.12345678 1.12345678)
In Main(), notice the creation and use of two different CultureInfo instances. First, the
ComplexNumber is output using American cultural formatting, and second, using German
cultural formatting. In both cases, you output the string using only five significant digits,
and System.Double??™s implementation of IFormattable.ToString() even rounds the result
as expected. Finally, the Object.ToString() override is implemented to defer to the
IFormattable.ToString method using the G (general) format.
IFormattable provides the clients of your objects with powerful capabilities when they
have specific formatting needs for your objects. However, that power comes at an implementation
cost. Implementing IFormattable.ToString() can be a very detail-oriented task that
takes a lot of time and attentiveness.
Is the Object Convertible?
VB provides support for converting instances of simple built-in value types, such as Integer
and Long, from one type to another.
Pages:
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534