Skip to content

Commit b97c9d7

Browse files
HaroldPetersInskippmattbaileyuk
authored andcommitted
Add files via upload
1 parent 8e4abd8 commit b97c9d7

File tree

1 file changed

+172
-0
lines changed

1 file changed

+172
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
id: version-1.8.0-numeric-functions
3+
title: Numeric functions
4+
sidebar_label: Numeric Functions
5+
original_id: numeric-functions
6+
---
7+
8+
## `$number()`
9+
__Signature:__ `$number(arg)`
10+
11+
Casts the `arg` parameter to a number using the following casting rules
12+
- Numbers are unchanged
13+
- Strings that contain a sequence of characters that represent a legal JSON number are converted to that number
14+
- Boolean `true` casts to `1`, Boolean `false` casts to `0`
15+
- All other values cause an error to be thrown.
16+
17+
If `arg` is not specified (i.e. this function is invoked with no arguments), then the context value is used as the value of `arg`.
18+
19+
__Examples__
20+
- `$number("5")` => `5`
21+
- `["1", "2", "3", "4", "5"].$number()` => `[1, 2, 3, 4, 5]`
22+
23+
24+
## `$abs()`
25+
__Signature:__ `$abs(number)`
26+
27+
Returns the absolute value of the `number` parameter, i.e. if the number is negative, it returns the positive value.
28+
29+
If `number` is not specified (i.e. this function is invoked with no arguments), then the context value is used as the value of `number`.
30+
31+
__Examples__
32+
- `$abs(5)` => `5`
33+
- `$abs(-5)` => `5`
34+
35+
## `$floor()`
36+
__Signature:__ `$floor(number)`
37+
38+
Returns the value of `number` rounded down to the nearest integer that is smaller or equal to `number`.
39+
40+
If `number` is not specified (i.e. this function is invoked with no arguments), then the context value is used as the value of `number`.
41+
42+
__Examples__
43+
- `$floor(5)` => `5`
44+
- `$floor(5.3)` => `5`
45+
- `$floor(5.8)` => `5`
46+
- `$floor(-5.3)` => `-6`
47+
48+
49+
## `$ceil()`
50+
__Signature:__ `$ceil(number)`
51+
52+
Returns the value of `number` rounded up to the nearest integer that is greater than or equal to `number`.
53+
54+
If `number` is not specified (i.e. this function is invoked with no arguments), then the context value is used as the value of `number`.
55+
56+
__Examples__
57+
- `$ceil(5)` => `5`
58+
- `$ceil(5.3)` => `6`
59+
- `$ceil(5.8)` => `6`
60+
- `$ceil(-5.3)` => `-5`
61+
62+
63+
## `$round()`
64+
__Signature:__ `$round(number [, precision])`
65+
66+
Returns the value of the `number` parameter rounded to the number of decimal places specified by the optional `precision` parameter.
67+
68+
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
69+
70+
This function uses the [Round half to even](https://en.wikipedia.org/wiki/Rounding#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.
71+
72+
__Examples__
73+
- `$round(123.456)` => `123`
74+
- `$round(123.456, 2)` => `123.46`
75+
- `$round(123.456, -1)` => `120`
76+
- `$round(123.456, -2)` => `100`
77+
- `$round(11.5)` => `12`
78+
- `$round(12.5)` => `12`
79+
- `$round(125, -1)` => `120`
80+
81+
## `$power()`
82+
__Signature:__ `$power(base, exponent)`
83+
84+
Returns the value of `base` raised to the power of `exponent` (<code>base<sup>exponent</sup></code>).
85+
86+
If `base` is not specified (i.e. this function is invoked with one argument), then the context value is used as the value of `base`.
87+
88+
An error is thrown if the values of `base` and `exponent` lead to a value that cannot be represented as a JSON number (e.g. Infinity, complex numbers).
89+
90+
__Examples__
91+
- `$power(2, 8)` => `256`
92+
- `$power(2, 0.5)` => `1.414213562373`
93+
- `$power(2, -2)` => `0.25`
94+
95+
## `$sqrt()`
96+
__Signature:__ `$sqrt(number)`
97+
98+
Returns the square root of the value of the `number` parameter.
99+
100+
If `number` is not specified (i.e. this function is invoked with one argument), then the context value is used as the value of `number`.
101+
102+
An error is thrown if the value of `number` is negative.
103+
104+
__Examples__
105+
- `$sqrt(4)` => `2`
106+
- `$sqrt(2)` => `1.414213562373`
107+
108+
## `$random()`
109+
__Signature:__ `$random()`
110+
111+
Returns a pseudo random number greater than or equal to zero and less than one (<code>0 &#8804; n < 1</code>)
112+
113+
__Examples__
114+
- `$random()` => `0.7973541067127`
115+
- `$random()` => `0.4029142127028`
116+
- `$random()` => `0.6558078550072`
117+
118+
119+
## `$formatNumber()`
120+
__Signature:__ `$formatNumber(number, picture [, options])`
121+
122+
Casts the `number` to a string and formats it to a decimal representation as specified by the `picture` string.
123+
124+
The behaviour of this function is consistent with the XPath/XQuery function [fn:format-number](https://www.w3.org/TR/xpath-functions-31/#func-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](https://www.w3.org/TR/xpath-functions-31/#syntax-of-picture-string) as fn:format-number.
125+
126+
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](https://www.w3.org/TR/xpath-functions-31/#defining-decimal-format) section of the XPath F&O 3.1 specification.
127+
128+
__Examples__
129+
130+
- `$formatNumber(12345.6, '#,###.00')` => `"12,345.60"`
131+
- `$formatNumber(1234.5678, "00.000e0")` => `"12.346e2"`
132+
- `$formatNumber(34.555, "#0.00;(#0.00)")` => `"34.56"`
133+
- `$formatNumber(-34.555, "#0.00;(#0.00)")` => `"(34.56)"`
134+
- `$formatNumber(0.14, "01%")` => `"14%"`
135+
- `$formatNumber(0.14, "###pm", {"per-mille": "pm"})` => `"140pm"`
136+
- `$formatNumber(1234.5678, "①①.①①①e①", {"zero-digit": "\u245f"})` => `"①②.③④⑥e②"`
137+
138+
139+
## `$formatBase()`
140+
__Signature:__ `$formatBase(number [, radix])`
141+
142+
Casts the `number` to a string and formats it to an integer represented in the number base specified by the `radix` argument. If `radix` is not specified, then it defaults to base 10. `radix` can be between 2 and 36, otherwise an error is thrown.
143+
144+
__Examples__
145+
146+
- `$formatBase(100, 2)` => `"1100100"`
147+
- `$formatBase(2555, 16)` => `"9fb"`
148+
149+
150+
## `$formatInteger()`
151+
__Signature:__ `$formatInteger(number, picture)`
152+
153+
Casts the `number` to a string and formats it to an integer representation as specified by the `picture` string.
154+
155+
The behaviour of this function is consistent with the two-argument version of the XPath/XQuery function [fn:format-integer](https://www.w3.org/TR/xpath-functions-31/#func-format-integer) 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-integer.
156+
157+
__Examples__
158+
159+
- `$formatInteger(2789, 'w')` => `"two thousand, seven hundred and eighty-nine"`
160+
- `$formatInteger(1999, 'I')` => `"MCMXCIX"`
161+
162+
## `$parseInteger()`
163+
__Signature:__ `$parseInteger(string, picture)`
164+
165+
Parses the contents of the `string` parameter to an integer (as a JSON number) using the format specified by the `picture` string.
166+
The picture string parameter has the same format as `$formatInteger`. Although the XPath specification does not have an equivalent
167+
function for parsing integers, this capability has been added to JSONata.
168+
169+
__Examples__
170+
171+
- `$parseInteger("twelve thousand, four hundred and seventy-six", 'w')` => `12476`
172+
- `$parseInteger('12,345,678', '#,##0')` => `12345678`

0 commit comments

Comments
 (0)