Brian Dunning's FileMaker Custom Functions

FormatNumber ( number ; format )

Non-recursive function returns a text string of the 'number' in the requested 'format'.

  Average rating: 4.0 (57 votes) Log in to vote

Lewis Lorenz   Lewis Lorenz - Show more from this author
Lorenz Companies
http://www.LorenzCom.com

Share on Facebook Share on Twitter

  Sample input:
FormatNumber ( 14725.3675 ; "$,.2 US" )
FormatNumber ( -14725.3675 ; "$(,.2) US\r0\" )
FormatNumber ( -14725.3675 ; "#,.\c255\" )
FormatNumber ( 1 ; "Yes||No" )
FormatNumber ( -59703600 ; \".-3-\" )
FormatNumber ( 5973600 ; "^" )
FormatNumber ( 59703600 ; "~0 kg" )
  Sample output:
$14,725.37 US
$(14,725.36) US [ rounding off ]
#-14,725.3675 [ text color is red ]
Yes
59704000-
5.9736e6
59.7036e6 kg

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

FormatNumber ( number ; format )

Returns a text string of the 'number' in the requested 'format'.

By Lewis C. Lorenz
Updated: 4/12/18

Non-recursive function allows complete FileMaker Pro number formatting using an intuitive, liberal format structure. Free-form formatting provides the user flexible control over what is included in the final formatted result. Escaped options can be used instead. Include the following elements in the format string in the order the user desires to produce a formatted number.

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) decimal portion of the 'number' is included.
0…n - (integer) specifies the rounding and truncation precision applied to the 'number':
0 - returns integer without decimal and decimal separator. Also, acts as a separator to allow positioning of negative signs and notations.
1…n - rounding or truncation precision of the formatted 'number' (negative affects integer; positive affects 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). (Dark delta is output.)
Minus sign, credit and delta symbols can be positioned on the left or right of the formatted 'number' by placing them to the left or right of an included decimal (.) or zero (0) separator in 'format'. Negative signs are placed at their default position when a decimal or zero is not included.

BOOLEAN FORMATTING:
|| - two pipe characters separate boolean values. True 'number' (non-zero/non-empty) returns left value; false 'number' (zero/empty) returns right value.

NOTATION FORMATTING:
% - percent sign. Inclusion in 'format' forces multiplication of the formatted '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 positioned on the left or right of the formatted 'number' by placing them on the left or right of an included decimal (.) or zero (0) separator in 'format'. Notations are placed at their default position when a decimal or zero is not included.

ESCAPED OPTIONS FORMATTING:
\T - (text) thousands separator (default: system).
\D - (text) decimal separator (default: system).
\R - (boolean) round or truncate 'number' to passed precision integer (true: round on; false: truncate on) (default: true).
\PI - (integer) round or truncate to this postive or negative integer precision (default: empty).
\Z - (boolean) show 'number' if zero (default: true).
\F - (boolean) fill (pad) decimal with zeros to length of passed precision (default: true).
\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).
\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).
\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 \\\\.

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 ( -59703600 ; \".-3-\" ) = 59704000-
FormatNumber ( 5973600 ; "^" ) = 5.9736e6
FormatNumber ( 59703600 ; "~0 kg" ) = 59.7036e6 kg

 

Comments

Juju   Juju, indonesia/bengkulu
Nov 4, 2015
help me
i have problem :
LV1.listitems.Clear
Call NisBobot
For i = 1 To LV.listitems.Count
For j = 1 To 7
z = FormatNumber(LV.listitems(i).ListSubItems(j), 2)
nNormal = FormatNumber(z / FormatNumber(nMax(j), 2), 2)
Set lst1 = LV1.listitems.Add(, , "")
LV1.listitems(i).SubItems(j) = nNormal
Next j
Next i

this message error " type mismacth"
please solution,...thank
 
Lewis Lorenz   Lewis Lorenz, Lorenz Companies
Nov 22, 2016
The format should be string.
Change: FormatNumber(LV.listitems(i).ListSubItems(j), 2)
to: FormatNumber(LV.listitems(i).ListSubItems(j), "2")
 
Daniel H.   Daniel H., United States
Apr 5, 2018
Needs an option to round up or down, so if 1.80, should round to 2, currently rounds everything down to 1.
 
Lewis Lorenz   Lewis Lorenz, Lorenz Companies
Apr 12, 2018
Updated function 4/12/2018 to adjust rounding and truncation formatting:

NUMBER FORMATTING:
0…n - (integer) specifies the rounding and truncation precision applied to the 'number':
0 - returns integer without decimal and decimal separator. Also, acts as a separator to allow positioning of negative signs and notations.
1…n - rounding or truncation precision of the formatted 'number' (negative affects integer; positive affects decimal).
ESCAPED OPTIONS FORMATTING:
\R - (boolean) round or truncate 'number' to passed precision integer (true: round on; false: truncate on) (default: true).
\PI - (integer) round or truncate to this postive or negative integer precision (default: empty).
 
Blair Phifer   Blair Phifer
May 11, 2021
Trying to use this function, and it doesn't seem to work with numbers that are in the hundreds of millions (123,456,789). Works fine with values that are 8 digits, but when the value has nine, there is no formatting until the field is selected. I am trying to get US Currency figures, and I used this format: $(,) . I do not need decimal places.

Love this function! Thanks for any assistance.
 
Blair Phifer   Blair Phifer
May 12, 2021
Solved my problem. I forgot to change the output of the calculation to "Text." Odd that it worked for all other numbers, just not the largest one.

Thanks!
 

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: