ArborX


format_num

Longname: format_num ber   Open the Documentation Legend
Released: 7.95 
Created: 7.95 
See also: format_seq var Technote: 0080

Summary
A keyword used with function variables: ^[funcname parameter(s)]. This syntax can be used anywhere that a regular 3B2 variable can be used: in show strings, in scripts, in expressions, etc. Function variables are a V8 feature that uniforms variable use and functions into a readable syntax. The 'format_num' function enables you to reorganise the format and appearance of number stings where 'num' is the number to be formatted, 'pattern' is the format pattern to be used, and 'params' are the optional parameter to configure how the format pattern is interpreted.
Example extract

The following show string will display the current value of the th command (text height).

 <?show ^[prop th]?>

Syntax (7.95)
format_num num:s patter:s  params:s?
num:s

The number string to be formatted.

pattern:

The number pattern to follow.

Character

Default char

Definition

zero-digit

0

Output a digit

digit

#

Output a digit, unless it is a redundant leading or trailing zero

decimal-separator

.

Separate the integer and fractional part of the number

grouping-separator

,

Separates groups of numbers

pattern-separator

;

Separates positive and negative format patterns

minus-sign

-

Negative number prefix

percent

%

Multiply by 100 and show as a percentage

per-mille

?

Multiply by 1000 and show as per-mille

apostrophe

'

Quotes special characters

Any other characters placed in the format pattern are output directly. The format pattern allows for specifying a different pattern for positive and negative values using the "pattern-separator" character (default ’;’). For example, the format pattern "#.00;(#.00)" will output "123.45" with "12345" and "(123.45)" with an input of "-12345". When using the grouping-separator, the number of digits per group will be set to the number of digits specified between the last grouping separator and the end of the integer.

Example

The following willl output a number in a European style format, with a comma as a decimal separator, and a full-stop as the grouping separator.

 ^[format_num 1234.5, "#.##0,00", "grouping-separator=’.’; decimal-separator=’,’"]

Will output "1.234,50".Note that the same format pattern without the third parameter specifiedwill output "1234.500".

Overriding the "zero-digit" will change what symbols are used instead of numbers, i.e.

 ^[format_num 1234.5, "#,##a.aa","zero-digit=’a’"]

Will output "b,cde.fa".

params:s

The special characters used in the format pattern are only set as a default.The third parameter for format_num allows the characters to be changed, to allow for localized numbering for example. Each of the special characters with the exception of the apostrophe can be overridden.

The third parameter is a single string containing a semi-colon separated list of values. It is only required to specify the values that need overriding. Note that changing the special characters affects the way the format pattern is processed in addition to changing the output. For example, if the grouping-separator is changed to a period (.), placing a comma in the format pattern will result in a comma being output as a normal character, the size of the group being determined by the period.

Example

Examples:

 ^[format_num 1234567,"#,###"]

Will output 1,234,567.

Number

Format Pattern

Result

12345

#,##0.00

12,345.00

1234.5

#,##0.00

1,234.50

123.456

#,##0.00

123,46

1234

###0.0###

1234.0

123.45

00000.000

00123.450

0.25

#00%

25%

1234

###0.0###

1234.0

12.34

£#.00CR;£#.00OD

£12.34CR

-12.34

£#.00CR;£#.00OD

£12.34OD

 <?show $^[format_num 1234567,"#,###"]>

Output 1,234,567

 <?show $^[format_num 12.34,"£#.00 CREDIT;£#.00 OVERDRAWN"]>

Output £12.34 CREDIT

 <?show $^[format_num -12.34,"£#.00 CREDIT;£#.00 OVERDRAWN"]>

Output £12.34 OVERDRAWN


Document created on 22-Nov-2004 (revision 1)