Skip to main content

$formatNumber

$formatNumber(number?, picture, options?) -> string
Compact type signature<n-so?:s>

If number is omitted, the current context value is used.

Browse categories

Source: JSONata

Documentation

Casts the number to a string and formats it to a decimal representation as specified by the picture string.

The behaviour of this function is consistent with the XPath/XQuery function fn:format-number as defined in the XPath F&O 3.1 specification. The picture string parameter defines how the number is formatted and has the same syntax as fn:format-number.

The optional third argument options is used to override the default locale specific formatting characters such as the decimal separator. If supplied, this argument must be an object containing name/value pairs specified in the decimal format section of the XPath F&O 3.1 specification.

Examples

Basic usage

  • $formatNumber(12345.6, '#,###.00') => "12,345.60"
  • $formatNumber(1234.5678, "00.000e0") => "12.346e2"
  • $formatNumber(34.555, "#0.00;(#0.00)") => "34.56"
  • $formatNumber(-34.555, "#0.00;(#0.00)") => "(34.56)"
  • $formatNumber(0.14, "01%") => "14%"
  • $formatNumber(0.14, "###pm", {"per-mille": "pm"}) => "140pm"
  • $formatNumber(1234.5678, "â‘ â‘ .â‘ â‘ â‘ eâ‘ ", {"zero-digit": "\u245f"}) => "â‘ â‘¡.③④⑥eâ‘¡"

Format an amount with a fixed number of decimals

Input

This example uses the claims-lines example input. The expression reads allowedAmount from the first claim line and formats it for display.

Example input

JSON
[
{
"claimId": "clm-2001",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 110,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2001",
"lineNumber": 2,
"serviceCode": "PROC-020",
"serviceDisplay": "imaging study",
"units": "1",
"unitPrice": 350,
"allowedAmount": 300,
"modifiers": [
"MOD-B",
"MOD-C"
]
},
{
"claimId": "clm-2001",
"lineNumber": 3,
"serviceCode": "PROC-030",
"serviceDisplay": "lab panel",
"units": 2,
"unitPrice": 40,
"allowedAmount": 70,
"modifiers": []
},
{
"claimId": "clm-2002",
"lineNumber": 1,
"serviceCode": "PROC-010",
"serviceDisplay": "office visit",
"units": 1,
"unitPrice": 125,
"allowedAmount": 115,
"modifiers": [
"MOD-A"
]
},
{
"claimId": "clm-2002",
"lineNumber": 2,
"serviceCode": "PROC-040",
"serviceDisplay": "counseling",
"units": 3,
"unitPrice": 60,
"allowedAmount": 150,
"modifiers": [
"MOD-D"
]
}
]

Expression

$formatNumber($[0].allowedAmount, "#,##0.00")

Result

"110.00"