Skip to main content

Format Number

The goal of format_number parameter is to help display numbers in a human-readable form.

fields:   #begin of fields section (inside View or Model)
- dimension: price
format_number: '$,.2f' # 1057.1258 >> $1 057.13
currency_prefix: $
currency_suffix: ''
sql: product_price

Syntax

Fields with result: number (implicitly or explicitly set) can accept format_number using special syntax of D3 format:

[[fill]align][sign][symbol][0][width][,][.precision][type]

If options are specified, they must preserve its order.

Fill, Align, Width

The align can be:

  • < - left alignment
  • ^ - center alignment
  • > - right alignment (default)

The fill can be any character. Fill can be specified only if align specified. It fills empty characters if value requires less characters than width. The width defines the minimum field width in characters. If not specified, then the width will be determined by the content.

[[fill]align][sign][symbol][0][width][,][.precision][type]

Inputformat_numberoutputdescription
5B<75BBBBBBset width to 7 and fill empty characters with B
5*^7***5***set width to 7 and fill empty characters with *
5A>7AAAAAA5set width to 7 and fill empty characters with A

Sign

The sign can be:

  • - - nothing for zero or positive and a minus sign for negative (default)
  • + - a plus sign for zero or positive and a minus sign for negative
  • ( - nothing for zero or positive and parentheses for negative

[[fill]align][sign][symbol][0][width][,][.precision][type]

Inputformat_numberoutput
5++5
-5+-5
5-5
-5--5
5(5
-5((5)

Symbol

The symbol can be:

  • $ - apply currency_prefix and currency_suffix
  • # - for binary, octal, or hexadecimal types, prefix by 0b, 0o, or 0x, respectively.

[[fill]align][sign][symbol][0][width][,][.precision][type]

Inputformat_numberoutputdescription
5$$5
  • currency_prefix: '$'
  • currency_suffix: ''
5$5
  • currency_prefix: ''
  • currency_suffix: ''
5$5$
  • currency_prefix: ''
  • currency_suffix: '$'
5$usd $
  • currency_prefix: 'usd '
  • currency_suffix: ''
5$5 usd
  • currency_prefix: ''
  • currency_suffix: ' usd'
5$abc 5 def
  • currency_prefix: 'abc '
  • currency_suffix: ' def'
5$5 units
  • currency_prefix: ''
  • currency_suffix: ' units'
100#b0b1100100
100#o0o144
100#x0x64

Zero

The zero 0 option enables zero-padding; this implicitly sets fill to 0.

[[fill]align][sign][symbol][0][width][,][.precision][type]

Inputformat_numberoutput
5070000005
123070000123
12345678907123456789

Comma

The comma , option enables the use of a group separator for thousands.

[[fill]align][sign][symbol][0][width][,][.precision][type]

Inputformat_numberoutput
1000000,1,000,000
-57023.55,-57,023.55
705,705

Precision and Type

The available type values are:

  • e - exponent notation
  • d - decimal notation, rounded to integer
  • % - multiply by 100, and then decimal notation with a percent sign
  • p - multiply by 100, round to significant digits, and then decimal notation with a percent sign
  • s - decimal notation with an SI prefix, rounded to significant digits
  • b - binary notation, rounded to integer
  • o - octal notation, rounded to integer
  • x - hexadecimal notation, using lower-case letters, rounded to integer
  • X - hexadecimal notation, using upper-case letters, rounded to integer
  • c - (not yet supported) converts the integer to the corresponding unicode character before printing
  • g - either decimal or exponent notation, rounded to significant digits
  • - (none) like g, but trim insignificant trailing zeros
  • f - fixed point notation
  • r - decimal notation, rounded to significant digits
  • n - shorthand for ,g

For the g, n and (none) types, decimal notation is used if the resulting string would have precision or fewer digits; otherwise, exponent notation is used. Depending on the type, the precision either indicates the number of digits that follow the decimal point (types f and %), or the number of significant digits (types (none), e, g, r, s and p). If the precision is not specified, it defaults to 6 for all types except (none), which defaults to 12. Precision is ignored for integer formats (types b, o, d, x, X and c).

[[fill]align][sign][symbol][0][width][,][.precision][type]

Exponential

Inputformat_numberoutput
2000e2.000000e+3
20003010e2.000301e+7
0.000001e1.000000e-6

Integer

Inputformat_numberoutput
1000.1d1000
1000.5d1001
-1.12d-1

Percentage

Inputformat_numberoutput
1%100.000000%
0.999%99.900000%
0.12%12.000000%
-0.12%-12.000000%
0.25.3%25.000%
1p100.000%
0.999p99.9000%
0.12p12.0000%
-0.12p-12.0000%
0.25.3p25.0%

Scientific

Inputformat_numberoutput
0.000001s1.00000µ
0.001s1.00000m
1s1.00000
1000s1.00000k
1000000s1.00000M

Binary

Inputformat_numberoutput
1b1
8b1000
16b10000
2012b11111011100

Octal

Inputformat_numberoutput
1o1
8o10
16o20
2012o3734

Hexadecimal

Inputformat_numberoutput
17x11
2012x7dc
17X11
2012X7DC

General

Inputformat_numberoutput
1000000000000g1.00000e+12
2000g2000.00
2000.0301g2000.03
0.00012g0.000120000
0.987654.1g1
0.987654.2g0.99
0.987654.3g0.988

None

Inputformat_numberoutput
10000000000001e+12
20002000
2000.03012000.0301
0.000120.00012
0.987654.11
0.987654.20.99
0.987654.30.988

Fixed

Inputformat_numberoutput
1000000000000f1000000000000.000000
2000f2000.000000
2000.0301f2000.030100
0.00012f0.000120
0.987654.1f1.0
0.987654.2f0.99
0.987654.3f0.988

Rounded

Inputformat_numberoutput
2000r2000.00
2000.0301r2000.03
0.00012r0.000120000
0.987654.1r1
0.987654.2r0.99
0.987654.3r0.988