This module contains helper functions to work with Numbers.

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.

Functions

fromBinary

fromBinary(binaryText: String): Number

Transforms from a binary number into a decimal number.

Parameters
Name Description

binaryText

The binary number represented in a String.

Example

This example shows how the toBinary behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import fromBinary from dw::core::Numbers
output application/json
---
{
    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(binaryText: Null): Null

Helper function that enables fromBinary to work with null value.

fromHex

fromHex(hexText: String): Number

Transforms a hexadecimal number into decimal number.

Parameters
Name Description

hexText

The hexadecimal number represented in a String.

Example

This example shows how the toBinary behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import fromHex from dw::core::Numbers
output application/json
---
{
    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(hexText: Null): Null

Helper function that enables fromHex to work with null value.

fromRadixNumber

fromRadixNumber(numberStr: String, radix: Number): Number

Transforms a number in the specified radix into decimal number

Parameters
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
8
%dw 2.0
import fromRadixNumber from dw::core::Numbers
output application/json
---
{
    a: fromRadixNumber("10", 2),
    b: fromRadixNumber("FF", 16)
}
Output
1
2
3
4
{
  "a": 2,
  "b": 255
}

toBinary

toBinary(number: Number): String

Transforms a decimal number into a binary number.

Parameters
Name Description

number

The input number.

Example

This example shows how the toBinary behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import toBinary from dw::core::Numbers
output application/json
---
{
    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(number: Null): Null

Helper function that enables toBinary to work with null value.

toHex

toHex(number: Number): String

Transforms a decimal number into a hexadecimal number.

Parameters
Name Description

number

The input number.

Example

This example shows how toHex behaves with different inputs.

Source
1
2
3
4
5
6
7
8
9
10
11
%dw 2.0
import toHex from dw::core::Numbers
output application/json
---
{
    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(number: Null): Null

Helper function that enables toHex to work with null value.

toRadixNumber

toRadixNumber(number: Number, radix: Number): String

Transforms a decimal number into a number string in other radix.

Parameters
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
8
%dw 2.0
import toRadixNumber from dw::core::Numbers
output application/json
---
{
    a: toRadixNumber(2, 2),
    b: toRadixNumber(255, 16)
}
Output
1
2
3
4
{
  "a": "10",
  "b": "ff"
}