FormatNumber
FormatNumber ( number , format )
FormatNumber is a one-pass FileMaker Pro custom function that formats a number using intuitive, free-form format options that completely mimic FileMaker Pro’s number data type formatting. Formatting options can also be specified via escaped options. Returns text string of number in the requested format.
Parameters
- number = The number to be formatted.
- format = Text string specifying the returned format of the number.
Examples
- FormatNumber ( 14725.3675, “$,.2 US” ) = $14,725.37 US
- FormatNumber ( -14725.3675, “$(,.2) US\r0\” ) = $(14,725.36) US [rounding off]
- FormatNumber ( -14725.3675, “#,.\c255\” ) = #-14,725.3675 [text color is red]
- FormatNumber ( 1, “Yes||No” ) = Yes
- FormatNumber ( 5973600, “^” ) = 5.9736e6
- FormatNumber ( 59703600, “~0 kg” ) = 59.7036e6 kg [engineering notation]
HELP
Passing ‘?’ as the only content of ‘format’ returns a shortened version of this text.
Number Formatting:
- , – (thousands separator) integer portion of the number is “thousands separated”.
- . – (decimal separator) displays of the decimal portion of the number.
- # – (number) specifies the rounding precision (controls the count of decimal number digits to display):
- 0 – number decimal and decimal separator is not displayed. Enables positioning of negative signs and notations.
- 1…n – Truncation and rounding precision of the number decimal.
- . – decimal separator only, with no number, displays the unaltered number decimal.
- ^ – (caret) formats number in scientific E notation.
- ~ – (tilde) formats number in engineering E notation.
Negative Sign Formatting:
- – – minus sign (default) (default: left).
- <> – angle brackets (default: left and rigth).
- () – parentheses (default: left and right).
- CR – credit symbol (default: right).
- ∆ – delta symbol (default: left). (Black delta is output.)
Minus sign, credit and delta symbols can be displayed on the left or right of the formatted number by placing them to the left or right of an included decimal separator, or zero, in ‘format’. Negative signs are placed at their default location when no decimal separator is included.
Boolean Formatting:
- || – two pipe characters divide boolean results. True number returns left side. False number returns right side.
Notation Formatting:
- % – percent sign. Inclusion in ‘format’ forces multiplication of the number by 100 (default: right).
- $, £, #, etc. – dollar, pound, hash, and any other characters, can be included in the formatted number (default: left).
Percent, currency and other notations can be placed on either side of the formatted number depending on whether you place them at the left or right of an included decimal separator, or zero, in ‘format’. Notations are placed at their default position when no decimal separator is included. The user must insure that the notations in ‘format’ produces the desired formatted result.
Escaped Options Formatting:
- \T – (text) thousands separator (default: system).
- \D – (text) decimal separator (default: system).
- \R – (boolean) round number to passed precision (default: true).
- \Z – (boolean) show number if zero (default: true).
- \F – (boolean) fill (pad) decimal with zeros to length of passed precision (default: true).
- \SL – (text) negative sign (-, <>, (), CR, ∆) appearing on left side of number (default: minus sign).
- \SR – (text) negative sign (-, <>, (), CR, ∆) appearing on right side of number (default: empty).
- \PL – (boolean) proximity of negative sign on left side is next to number (default: true).
- \PR – (boolean) proximity of negative sign on right side is next to number (default: true).
- \NL – (text) notation sign ($, %, #, etc.) to appear on left side of number (default: empty).
- \NR – (text) notation sign ($, %, #, etc.) to appear on right side of number (default: empty).
- \C – (integers) comma-separated RGB number text (#,#,#) to color negative number (default: black).
- \X – (integer) exponentiation formatting: 0 = none, 1 = scientific, 2 = engineering (default: 0).
- \BP – (text) positive response to boolean formatting.
- \BN – (text) negative response to boolean formatting.
Escaped options override free-form format, defaults and add formatting. Enclose each option with escape (backslash) characters \\\\.
REVISION NOTES:
Revised February 11, 2014. Added code to process numbers input in scientific notation format and to return numbers in scientific and engineering notation format. Reformatted the boolean separator to be two pipe characters and added escape options to allow setting Boolean positive and negative values. Optimized the code to remove redundancies, decrease the overall size, and speed up processing. Restructured the help text to make it more concise so that it can be used easier in tool tip popups. Also, added code to determine system decimal and thousands separators.
FormatNumber code: Copy the selected code (click on Select All to highlight all the code) to the clipboard and paste it into a newly created Custom Function. Download code as a text file from here.
Comments
FormatNumber — No Comments
HTML tags allowed in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>