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 GetNumberFormat to format a number using locale defaults or custom formatting
Description
Keywordsformat number, GetNumberFormat, locale
CategoriesStrings, Software Engineering
 
The GetNumberFormat API function formats a string. This example lets you set the locale ID to LOCALE_SYSTEM_DEFAULT, LOCALE_USER_DEFAULT, or 0. If you set it to 0, the program lets you specify the number of digits after the decimal separator, whether to use a leading zero if the number has no whole part, the number of digits per grouping, the thousands and decimal separators, and the method for handling negative numbers (e.g. -1 versus (1) versus 1-).

GetNumberFormat places the formatted result in a buffer and returns the length of the buffer.

 
' Format a numeric string.
Private Function FormatString(ByVal numeric_string As _
    String, ByVal locale_id As Long, ByVal num_digits As _
    Integer, ByVal leading_zero As Boolean, ByVal _
    num_per_group As Integer, ByVal decimal_separator As _
    String, ByVal thousands_separator As String, ByVal _
    negative_order As Long) As String
Dim fmt As NUMBERFMT
Dim buf As String * 200
Dim buflen As Integer

    With fmt
        .NumDigits = num_digits
        If leading_zero Then
            .LeadingZero = 1
        Else
            .LeadingZero = 0
        End If
        .Grouping = num_per_group
        .lpDecimalSep = decimal_separator
        .lpThousandSep = thousands_separator
        .NegativeOrder = negative_order
    End With

    If locale_id = 0 Then
        buflen = GetNumberFormat(locale_id, 0, _
            numeric_string, fmt, buf, Len(buf))
    Else
        buflen = GetNumberFormat(locale_id, 0, _
            numeric_string, ByVal 0&, buf, Len(buf))
    End If

    FormatString = Left$(buf, buflen)
End Function
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated