Please enable JavaScript to view this site.

 

str = FormatNumber(sFormat,nNumber)

 

This macro function formats a number nNumber according to rules set for thousands separators, negative numbers, zero values and blank digits, in the parameter sFormat.

sOut = FormatNumber('#,##0', nPageNumber)

The format string is made up of :

 

0

Digit place holder. If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the output string. Otherwise, a '0' is stored in that position in the output string.

#

Digit placeholder. If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is stored in that position in the output string.

,

Thousand separator. If the format string contains one or more ',' characters, the output will have thousand separators inserted between each group of three digits to the left of the decimal point. The placement and number of ',' characters in the format string does not affect the output, except to indicate that thousand separators are wanted. The actual character used as a the thousand separator in the output is determined by the ThousandSeparator global variable. The default value of ThousandSeparator is specified in the Number Format of the International section in the Windows Control Panel.

 

Remarks

 

Characters enclosed in double quotes are output as-is, and do not affect formatting.

';' Separates sections for positive, negative, and zero numbers in the format string.

That this function depends on the settings of Windows on your workstation to decide what the thousands separator is, which would limit the use of this function to text for internal use, usually outside of the trim area.

The location of the '0' in the format string determines the number of digits that are always present in the output string.

 

Multiple Sections

 

To allow different formats for positive, negative, and zero values, the format string can contain between one and three sections separated by semicolons.

 

One section: The format string applies to all values.

Two sections: The first section applies to positive values and zeros, and the second section applies to negative values.

Three sections: The first section applies to positive values, the second applies to negative values, and the third applies to zeros.

 

Examples

 

If the section for negative values or the section for zero values is empty, that is if there is nothing between the semicolons that delimit the section, the section for positive values is used instead.

 

Consider the numbers 1234, -1234 and 0 using the following formats :

 

'' (blank)

1234

-1234

0.

'0'

1234

-1234

0.

'#'

1234

-1234

(nothing)

'#,##0'

1,234

-1,234

0

'#,##0;(#,##0)'

1,234

(1,234)

0

'#,##0;;Zero'

1,234

-1,234

Zero

 

See also: FormatDate, FormatParagraph, FormatText, and FormatTime.

 


Topic 108260, last updated on 18-Apr-2020