%dw 2.0
import * from dw::util::Coercions
import * from dw::Runtime
output application/dw
---
{
a: toNumber("0.005", [{format: "seconds"}, {format: ".00"}]),
b: try(() -> toNumber("0.005", [{format: "seconds"}])).error.message
}
toNumber
toNumber(str: String, formatters: Array<Formatter>): Number
Transforms a String value into a Number value using the first Formatter that
matches with the given value to transform.
Introduced in DataWeave version 2.5.0.
Parameters
| Name | Type | Description |
|---|---|---|
|
String |
The |
|
Array<DatesFormatter> |
The |
Example
This example shows how toNumber behaves with different inputs.
It produces output in the application/dw format.
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 |
|---|---|
|
The |
|
The unit of time ( |
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 |
|---|---|
|
The |
|
The unit to apply to the specified |
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 |
|---|---|
|
The |
|
Optional formatting to apply to the |
|
Optional ISO 3166 country code to use, such as |
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])
}



