Declare Function GetNumberFormat Lib "kernel32.dll" Alias "GetNumberFormatA" (ByVal Locale As Long, ByVal dwFlags As Long, ByVal lpValue As String, lpFormat As Any, ByVal lpNumberStr As String, ByVal cchNumber As Long) As Long
GetNumberFormat formats a number for display. By default, the function formats the number according to the specified locale's settings. However, custom formatting preferences can instead be used. The end result of GetNumberFormat is a number displayed according to the user's preferences.
If an error occured, the function returns 0 (use GetLastError to get the error code). If successful, the function returns the number of characters copied into the string passed as lpNumberStr, not including the terminating null character.
When passing 0 for the lpFormat parameter, the expression ByVal CLng(0) must be used. See the example code for a demonstration of this.
Const LOCALE_SYSTEM_DEFAULT = &H400
Const LOCALE_USER_DEFAULT = &H800
Const LOCALE_NOUSEROVERRIDE = &H80000000
' This code is licensed according to the terms and conditions listed here.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type NUMBERFMT
NumDigits As Long
LeadingZero As Long
Grouping As Long
lpDecimalSep As String
lpThousandSep As String
NegativeOrder As Long
End Type
Public Declare Function GetNumberFormat Lib "kernel32.dll" Alias "GetNumberFormatA" _
(ByVal Locale As Long, ByVal dwFlags As Long, ByVal lpValue As String, lpFormat _
As Any, ByVal lpNumberStr As String, ByVal cchNumber As Long) As Long
Const LOCALE_USER_DEFAULT = &H800
' Display the number -1,234,567.89 according to two formatting rules.
' 1. Use the format specified by the current user locale.
' 2. Use a custom format specified by a structure passed to the function.
Dim nft As NUMBERFMT ' custom formatting settings
Dim formatted As String ' receives the formatted number strings
Dim strlen As Long ' the length of the formatted string
' Display the number formatted according to the current locale.
formatted = Space(256)
strlen = GetNumberFormat(LOCALE_USER_DEFAULT, 0, "-1234567.89", ByVal CLng(0), _
formatted, Len(formatted))
formatted = Left(formatted, strlen)
Debug.Print "User locale format: "; formatted
' Now display according the format we specify below.
With nft
' Display three digits after the decimal point.
.NumDigits = 3
' Display zeros after the decimal point.
.LeadingZero = 1
' Group every three digits to the left of the decimal.
.Grouping = 3
' Use a comma to as the decimal point (like they do in France and Spain).
.lpDecimalSep = ","
' Likewise, use a period as the grouping separator.
.lpThousandSep = "."
' Put the negative sign immediately after the number.
.NegativeOrder = 3
End With
formatted = Space(256)
strlen = GetNumberFormat(LOCALE_USER_DEFAULT, 0, "-1234567.89", nft, formatted, Len(formatted))
formatted = Left(formatted, strlen)
Debug.Print "Custom format: "; formatted
Back to the Function list.
Back to the Reference section.
Last Modified: April 16, 2000
This page is copyright © 2000 Paul Kuliniewicz.
Copyright Information Revised October 29, 2000
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getnumberformat.html