Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse custom formatting strings to format Doubles in VB .NET
DescriptionThis example shows how to use custom formatting strings to format Doubles in VB .NET.
Keywordsformat, format specifier, string, VB.NET
CategoriesVB.NET, Strings
 
The Double class's ToString method formats a Double value. For example, the following code displays a value as currency with a thousands separator and three digits after the decimal place.

    Debug.WriteLine(value.ToString("$#,#.000"))

The following example displays positive values as currency values, negative values in parentheses, and zero as the string Zero.

    Debug.WriteLine(value.ToString("$#,#.#;($#,#.#);Zero"))

This example program lets you enter values and enter or pick format strings to see the results. When you change the value or format string, the ShowValue routine displays the result. It simply converts the value into a Double and passes its ToString method the format string.

 
Private Sub ShowValue()
    Try
        Dim value As Double = Double.Parse(txtValue.Text)
        txtResult.Text = value.ToString(cboFormat.Text)
    Catch ex As Exception
        txtResult.Text = ""
    End Try
End Sub
 
Experiment with it and consult the online help about format strings to learn more.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated