---
schema_version: "1"
id: 50c92e9f3efb69418205c5f51dd364d252e31adb786a984e52712db08ac06ed1
title: toNumber
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.12/dw-coercions-functions-tonumber.md
canonical_url: https://docs.mulesoft.com/dataweave/2.12/dw-coercions-functions-tonumber
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 9c2b5bc462720b374291537864b5b8ed184169682cc5cea22f4a7c179c9a616d
description: MuleSoft Documentation Site
release: "2.12"
latest: false
---
---
product: DataWeave
version: 2.12
---

# toNumber

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

## 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

<table><colgroup><col> <col> <col></colgroup><thead><tr><th>Name</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p><code>str</code></p></td><td><p>String</p></td><td><p>The <code>String</code> value to transform into a <code>Number</code> value.</p></td></tr><tr><td><p><code>formatters</code></p></td><td><p>Array&lt;DatesFormatter&gt;</p></td><td><p>The <code>array</code> of formatting to use on the <code>Number</code> value.</p></td></tr></tbody></table>

### Example

This example shows how `toNumber` behaves with different inputs. It produces output in the
`application/dw` format.

#### Source

```DataWeave
%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
}
```

#### Output

```DataWeave
{
  a: 0.005 as Number {format: ".00"},
  b: "Could not find a valid formatter for '0.005'"
}
```

## 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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>dateTime</code></p></td><td><p>The <code>DateTime</code> value to transform into a <code>Number</code> value.</p></td></tr><tr><td><p><code>unit</code></p></td><td><p>The unit of time (<code>"milliseconds"</code> or <code>"seconds"</code>) to use Given a <code>null</code> value, the function uses <code>"seconds"</code>.</p></td></tr></tbody></table>

### Example

This example shows how `toNumber` behaves with different inputs.

#### Source

```DataWeave
%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

```DataWeave
{
  "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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>period</code></p></td><td><p>The <code>Period</code> value to transform into a <code>Number</code> value.</p></td></tr><tr><td><p><code>unit</code></p></td><td><p>The unit to apply to the specified <code>period</code>: <code>hours</code>, <code>minutes</code>, <code>seconds</code>, <code>milliseconds</code>, or <code>nanos</code>.</p></td></tr></tbody></table>

### Example

This example shows how `toNumber` behaves with different inputs.

#### Source

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

#### Output

```Json
{
  "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

<table><colgroup><col> <col></colgroup><thead><tr><th>Name</th><th>Description</th></tr></thead><tbody><tr><td><p><code>value</code></p></td><td><p>The <code>String</code> or <code>Key</code> value to transform into a <code>Number</code> value.</p></td></tr><tr><td><p><code>format</code></p></td><td><p>Optional formatting to apply to the <code>value</code>. A <code>format</code> accepts <code>#</code> or <code>0</code> (but not both) as placeholders for <em>decimal</em> values and a single whole number that is less than <code>10</code>. Only one decimal point is permitted. A <code>null</code> or empty <code>String</code> value has no effect on the <code>Number</code> value. Other characters produce an error.</p></td></tr><tr><td><p><code>locale</code></p></td><td><p>Optional ISO 3166 country code to use, such as <code>US</code>, <code>AR</code>, or <code>ES</code>. A <code>null</code> or absent value uses your JVM default.</p></td></tr></tbody></table>

### Example

This example shows how `toNumber` behaves with different inputs.

#### Source

```DataWeave
%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

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