Skip to main content

$round

$round(number?, precision?) -> number
Compact type signature<n-n?:n>

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

Browse categories

Source: JSONata

Documentation

Returns the value of the number parameter rounded to the number of decimal places specified by the optional precision parameter.

The precision parameter (which must be an integer) species the number of decimal places to be present in the rounded number. If precision is not specified then it defaults to the value 0 and the number is rounded to the nearest integer. If precision is negative, then its value specifies which column to round to on the left side of the decimal place

This function uses the Round half to even strategy to decide which way to round numbers that fall exactly between two candidates at the specified precision. This strategy is commonly used in financial calculations and is the default rounding mode in IEEE 754.

Examples

Basic usage

  • $round(123.456) => 123
  • $round(123.456, 2) => 123.46
  • $round(123.456, -1) => 120
  • $round(123.456, -2) => 100
  • $round(11.5) => 12
  • $round(12.5) => 12
  • $round(125, -1) => 120

Round a derived value for display

Input

This example uses the claims-lines example input. The expression reads allowedAmount from the second claim line and applies the rounding step.

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

$round($[1].allowedAmount / 7, 2)

Result

42.86