The formatNumber()
function formats a number as a string with additional stylistic formatting, such as commas or currency formatting.
Use it to display a number with commas at each thousands-place, add a dollar sign, display a number with “humanized” formatting (e.g. 12M
instead of 12,000,000
), etc – without needing to use complex regular expressions. This function is especially useful in formulas where you need to display a number alongside additional text.
formatNumber(number, format?, precision?)
number.formatNumber(format?, precision?)
/* "?" denotes an optional argument */
Code language: JavaScript (javascript)
Heads up: formatNumber()
returns a string, meaning you can’t do math on its output . In Table views, the Calculate function also won’t offer arithmetic options.
formatNumber()
accepts two optional arguments:
- Format – the format/style to be used for the formatted number. See the available options in the table below.
- Precision – the number of decimal places (0-12) to display in the final, formatted number. Adds as many
0
characters to numbers without decimals.
Here are all the available format options, with an example for each. Note that formatNumber()
defaults to “commas” if you don’t pass any arguments.
Format Option | Example Input | Example Output |
---|---|---|
commas | 12345 | 12,345 |
percent | 0.856 | 85.6% |
humanize or compact | 1234567 | 1.2M |
bytes_decimal | 12345678 | 12.35 MB |
bytes_binary or bytes | 12345678 | 12.06 KiB |
usd or dollar | 1234.56 | $1,234.56 |
aud | 1234.56 | A$1,234.56 |
cad | 1234.56 | CA$1,234.56 |
sgd | 1234.56 | SGD 1,234.56 |
eur or euro | 1234.56 | €1,234.56 |
gbp | 1234.56 | £1,234.56 |
jpy or yen | 1234.56 | ¥1,235 |
rub | 1234.56 | RUB 1,234.56 |
inr | 1234.56 | ₹1,234.56 |
krw | 1234.56 | ₩1,235 |
cny | 1234.56 | CN¥1,234.56 |
brl | 1234.56 | R$1,234.56 |
try | 1234.56 | TRY 1,234.56 |
idr | 1234.56 | IDR 1,234.56 |
chf | 1234.56 | CHF 1,234.56 |
hkd | 1234.56 | HK$1,234.56 |
nzd | 1234.56 | NZ$1,234.56 |
sek | 1234.56 | SEK 1,234.56 |
nok | 1234.56 | NOK 1,234.56 |
mxn | 1234.56 | MX$1,234.56 |
zar | 1234.56 | ZAR 1,234.56 |
twd | 1234.56 | NT$1,234.56 |
dkk | 1234.56 | DKK 1,234.56 |
pln | 1234.56 | PLN 1,234.56 |
thb | 1234.56 | THB 1,234.56 |
huf | 1234.56 | HUF 1,234.56 |
czk | 1234.56 | CZK 1,234.56 |
ils | 1234.56 | ₪1,234.56 |
clp | 1234.56 | CLP 1,235 |
php | 1234.56 | ₱1,234.56 |
aed | 1234.56 | AED 1,234.56 |
cop | 1234.56 | COP 1,234.56 |
sar | 1234.56 | SAR 1,234.56 |
myr | 1234.56 | MYR 1,234.56 |
ron | 1234.56 | RON 1,234.56 |
ars | 1234.56 | ARS 1,234.56 |
uyu | 1234.56 | UYU 1,234.56 |
pen | 1234.56 | PEN 1,234.56 |
A couple of additional notes on these formats:
humanize
will ignore anyprecision
argument provided.- The
bytes
choices do respect aprecision
value, but ignore significant decimal places. For example,formatNumber(1.2345, "bytes_decimal", 4)
will display1.2345 B
, butformatNumber(1, "bytes_decimal", 4)
will display1 B
, not1.0000 B
.
Example Formulas
1234.formatNumber() /* Output: 1,234 */
12345.formatNumber("commas", 2) /* Output: 12,345.00 */
1234567.formatNumber("humanize") /* Output: 1.2M */
"Revenue this quarter is " + prop("Revenue").formatNumber("usd")
/* Output: Revenue this quarter is $11,581,619.18 */
Code language: JavaScript (javascript)
You can find a database containing examples of every formatting option at this Notion page:
