This module contains helper functions to work with Number.
To use this module, you must import it to your DataWeave code, for example,
by adding the line import * from dw::core::Numbers
to the header of your
DataWeave script.
Since Version: 2.2.0
Functions
fromBinary
fromBinary(String): Number
Transforms from a binary number into a decimal number
Since Version: 2.2.0 ===== Parameters
[%header, cols="1,3"] |=== | Name | Description | `binaryText` | The binary number represented in a `String` |===
Example
This example shows how the toBinary
behaves under different inputs.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import fromBinary from dw::core::Numbers
---
{
a: fromBinary("-10"),
b: fromBinary("11111000111010111010110100101011100001001110000011010101100010111101001011100000100010011000011101100101101001111101111010110010010100110010100100000000000000000000000000000000000000000000000000000000000000"),
c: fromBinary(0),
d: fromBinary(null),
e: fromBinary("100"),
}
Output
1
2
3
4
5
6
7
{
"a": -2,
"b": 100000000000000000000000000000000000000000000000000000000000000,
"c": 0,
"d": null,
"e": 4
}
fromBinary(Null): Null
Helper function to make fromBinary
work with null value
fromHex
fromHex(String): Number
Transforms a hexadecimal number into decimal number.
Since Version: 2.2.0 ===== Parameters
[%header, cols="1,3"] |=== | Name | Description | `hexText` | The hexadecimal number represented in a `String`. |===
Example
This example shows how the toBinary
behaves under different inputs.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import fromHex from dw::core::Numbers
---
{
a: fromHex("-1"),
b: fromHex("3e3aeb4ae1383562f4b82261d969f7ac94ca4000000000000000"),
c: fromHex(0),
d: fromHex(null),
e: fromHex("f"),
}
Output
1
2
3
4
5
6
7
{
"a": -1,
"b": 100000000000000000000000000000000000000000000000000000000000000,
"c": 0,
"d": null,
"e": 15
}
fromHex(Null): Null
Helper function to make fromHex
work with null value
fromRadixNumber
fromRadixNumber(String, Number): Number
Transforms a number in the specified radix into decimal number
Since Version: 2.2.0 ===== Parameters
[%header, cols="1,3"] |=== | Name | Description | `numberText` | The number text | `radix` | The radix number |===
Example
This example shows how the fromRadixNumber
behaves under different inputs.
Source
1
2
3
4
5
6
7
%dw 2.0
import fromRadixNumber from dw::core::Numbers
---
{
a: fromRadixNumber("10", 2),
b: fromRadixNumber("FF", 16)
}
Output
1
2
3
4
{
"a": 2,
"b": 255
}
toBinary
toBinary(Number): String
Transforms a decimal number into a binary one.
Since Version: 2.2.0 ===== Parameters
[%header, cols="1,3"] |=== | Name | Description | `number` | The input number. |===
Example
This example shows how the toBinary
behaves under different inputs.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import toBinary from dw::core::Numbers
---
{
a: toBinary(-2),
b: toBinary(100000000000000000000000000000000000000000000000000000000000000),
c: toBinary(0),
d: toBinary(null),
e: toBinary(2),
}
Output
1
2
3
4
5
6
7
{
"a": "-10",
"b": "11111000111010111010110100101011100001001110000011010101100010111101001011100000100010011000011101100101101001111101111010110010010100110010100100000000000000000000000000000000000000000000000000000000000000",
"c": "0",
"d": null,
"e": "10"
}
toBinary(Null): Null
Helper function to make toBinary
work with null value.
toHex
toHex(Number): String
Transforms a decimal number into a hexadecimal one.
Since Version: 2.2.0 ===== Parameters
[%header, cols="1,3"] |=== | Name | Description | `number` | The input number. |===
Example
This example shows how the toHex
behaves under different inputs.
Source
1
2
3
4
5
6
7
8
9
10
%dw 2.0
import toHex from dw::core::Numbers
---
{
a: toHex(-1),
b: toHex(100000000000000000000000000000000000000000000000000000000000000),
c: toHex(0),
d: toHex(null),
e: toHex(15),
}
Output
1
2
3
4
5
6
7
{
"a": "-1",
"b": "3e3aeb4ae1383562f4b82261d969f7ac94ca4000000000000000",
"c": "0",
"d": null,
"e": "f"
}
toHex(Null): Null
Helper function to make toHex
work with null value.
toRadixNumber
toRadixNumber(Number, Number): String
Transforms a decimal number into a number string in other radix
Since Version: 2.2.0 ===== Parameters
[%header, cols="1,3"] |=== | Name | Description | `number` | The decimal number | `radix` | The radix of the result number |===
Example
This example shows how the toRadixNumber
behaves under different inputs.
Source
1
2
3
4
5
6
7
%dw 2.0
import toRadixNumber from dw::core::Numbers
---
{
a: toRadixNumber(2, 2),
b: toRadixNumber(255, 16)
}
Output
1
2
3
4
{
"a": "10",
"b": "ff"
}