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

# toNumber

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

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