Contact Us 1-800-596-4880

toNumber

toNumber(dateTime: DateTime, unit: MillisOrSecs | Null = null): Number

A variant of toNumber that transforms a DateTime value into a number of seconds or milliseconds, depending on the selected unit.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

dateTime

The DateTime value to transform into a Number value.

unit

The unit of time ("milliseconds" or "seconds") to use Given a null value, the function uses "seconds".

Example

This example shows how toNumber behaves with different inputs.

Source

%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
    epoch: toNumber(|2015-10-01T23:57:59Z|),
    millis: toNumber(|2015-10-01T23:57:59Z|, "milliseconds")
}

Output

{
  "epoch": 1443743879,
  "millis": 1443743879000
}

toNumber(period: Period, unit: PeriodUnits | Null = null): Number

A variant of toNumber that transforms a Period value into a number of hours, minutes, seconds, milliseconds or nanoseconds (nanos).

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

period

The Period value to transform into a Number value.

unit

The unit to apply to the specified period: hours, minutes, seconds, milliseconds, or nanos.

Example

This example shows how toNumber behaves with different inputs.

Source

%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
    toSecondsEx1: toNumber(|PT1H10M|, "seconds"),
    toSecondsEx2: toNumber(|PT1M7S|, "milliseconds")
}

Output

{
  "toSecondsEx1": 4200,
  "toSecondsEx2": 67000
}

toNumber(value: String | Key, format: String | Null = null, locale: String | Null = null): Number

A variant of toNumber that transforms a String or Key value into a Number value and that accepts a format and locale.

Introduced in DataWeave version 2.4.0.

Parameters

Name Description

value

The String or Key value to transform into a Number value.

format

Optional formatting to apply to the value. A format accepts # or 0 (but not both) as placeholders for decimal values and a single whole number that is less than 10. Only one decimal point is permitted. A null or empty String value has no effect on the Number value. Other characters produce an error.

locale

Optional ISO 3166 country code to use, such as US, AR, or ES. A null or absent value uses your JVM default.

Example

This example shows how toNumber behaves with different inputs.

Source

%dw 2.0
import * from dw::util::Coercions
var myKey = keysOf({"123" : "myValue"})
output application/json
---
 {
     "default": toNumber("1.0"),
     "withFormat": toNumber("0.005",".00"),
     "withLocal": toNumber("1,25","#.##","ES"),
     "withExtraPlaceholders": toNumber("5.55","####.####"),
     "keyToNumber": toNumber(myKey[0])
 }

Output

{
  "default": 1.0,
  "withFormat": 0.005,
  "withLocal": 1.25,
  "withExtraPlaceholders": 5.55,
  "keyToNumber": 123
}