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

# toString

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

## toString(number: Number, format: String | Null = null, locale: String | Null = null, roundMode: RoundingMode | Null = null): String

A variant of `toString` that transforms a `Number` value (whole or decimal) into a `String` value
and accepts a format, locale, and rounding mode value.

_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>number</code></p></td><td><div><div><p>The <code>Number</code> value to format.</p></div></div></td></tr><tr><td><p><code>format</code></p></td><td><div><div><p>The formatting to apply to the <code>Number</code> value. A <code>format</code> accepts <code>#</code> or <code>0</code> (but not both) as placeholders for <em>decimal</em> values, and 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. Most other values are treated as literals, but you must escape special characters, such as a dollar sign (for example, <code>\$</code>). Inner quotations must be closed and differ from the surrounding quotations.</p></div></div></td></tr><tr><td><p><code>roundMode</code></p></td><td><div><div><p>Optional parameter for rounding decimal values when the formatting presents a rounding choice, such as a format of <code>0.#</code> for the decimal <code>0.15</code>. The default is <code>HALF_UP</code>, and a <code>null</code> value returns behaves like <code>HALF_UP</code>. Only one of the following values is permitted:</p></div><div><ul><li><p><code>UP</code>: Always rounds away from zero (for example, <code>0.01</code> to <code>"0.1"</code> and <code>-0.01</code> to <code>"-0.1"</code>). Increments the preceding digit to a non-zero fraction and never decreases the magnitude of the calculated value.</p></li><li><p><code>DOWN</code>: Always rounds towards zero (for example, <code>0.19</code> to <code>"0.1"</code> and <code>-0.19</code> to <code>"-0.1"</code>). Never increments the digit before a discarded fraction (which truncates to the preceding digit) and never increases the magnitude of the calculated value.</p></li><li><p><code>CEILING</code>: Rounds towards positive infinity and behaves like <code>UP</code> if the result is positive (for example, <code>0.35</code> to <code>"0.4"</code>). If the result is negative, this mode behaves like <code>DOWN</code> (for example, <code>-0.35</code> to <code>"-0.3"</code>). This mode never decreases the calculated value.</p></li><li><p><code>FLOOR</code>: Rounds towards negative infinity and behaves like DOWN if the result is positive (for example, <code>0.35</code> to <code>"0.3"</code>). If the result is negative, this mode behaves like <code>UP</code> (for example, <code>-0.35</code> to <code>"-0.4"</code>). The mode never increases the calculated value.</p></li><li><p><code>HALF_UP</code>: Default mode, which rounds towards the nearest "neighbor" unless both neighbors are equidistant, in which case, this mode rounds up. For example, <code>0.35</code> rounds to <code>"0.4"</code>, <code>0.34</code> rounds to <code>"0.3"</code>, and <code>0.36</code> rounds to <code>"0.4"</code>. Negative decimals numbers round similarly. For example, <code>-0.35</code> rounds to `"-0.4`".</p></li><li><p><code>HALF_DOWN</code>: Rounds towards the nearest numeric "neighbor" unless both neighbors are equidistant, in which case, this mode rounds down. For example, <code>0.35</code> rounds to <code>"0.3"</code>, <code>0.34</code> rounds to <code>"0.3"</code>, and <code>0.36</code> rounds to <code>"0.4"</code>. Negative decimals numbers round similarly. For example, <code>-0.35</code> rounds to <code>"-0.3"</code>.</p></li><li><p><code>HALF_EVEN</code>: For decimals that end in a <code>5</code> (such as, <code>1.125</code> and <code>1.135</code>), the behavior depends on the number that precedes the <code>5</code>. <code>HALF_EVEN</code> rounds up when the next-to-last digit before the <code>5</code> is an odd number but rounds down when the next-to-last digit is even. For example, <code>0.225</code> rounds to <code>"0.22"</code>, <code>0.235</code> <em>and</em> <code>0.245</code> round to <code>"0.24"</code>, and <code>0.255</code> rounds to <code>"0.26"</code>. Negative decimals round similarly, for example, <code>-0.225</code> to <code>"-0.22"</code>. When the last digit is not <code>5</code>, the setting behaves like <code>HALF_UP</code>. Rounding of monetary values sometimes follows the <code>HALF_EVEN</code> pattern.</p></li></ul></div></div></td></tr></tbody></table>

### Example

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

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
    a: toString(1.0),
    b: toString(0.005,".00"),
    c: toString(0.035,"#.##","ES"),
    d: toString(0.005,"#.##","ES","HALF_EVEN"),
    e: toString(0.035,"#.00",null,"HALF_EVEN"),
    f: toString(1.1234,"\$.## 'in my account'")
}
```

#### Output

```Json
{
  "a": "1",
  "b": ".01",
  "c": "0,04",
  "d": "0",
  "e": ".04",
  "f": "$1.12 in my account"
}
```

## toString(date: Date | DateTime | LocalDateTime | LocalTime | Time, format: String | Null = null, locale: String | Null = null): String

A variant of `toString` that transforms a `Date`, `DateTime`, `LocalTime`, `LocalDateTime`, or
`Time` value into a `String` value.

_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>date</code></p></td><td><div><div><p>The <code>Date</code>, <code>DateTime</code>, <code>LocalTime</code>, <code>LocalDateTime</code>, or <code>Time</code> value to coerce to a <code>String</code> type.</p></div></div></td></tr><tr><td><p><code>format</code></p></td><td><div><div><p>The ISO-8601 formatting to use on the date or time. For example, this parameter accepts character patterns based on the Java 8 <code>java.time.format</code>. A <code>null</code> value has no effect on the value. Defaults:</p></div><div><ul><li><p><code>Date</code> example: <code>2011-12-03</code> (equivalent format: <code>uuuu-MM-dd</code>)</p></li><li><p><code>DateTime</code> example: <code>2011-12-03T10:15:30.000000+01:00</code> (equivalent format: <code>uuuu-MM-dd HH:mm:ssz</code>)</p></li><li><p><code>LocalDateTime</code> example: <code>2011-12-03T10:15:30.000000</code> (equivalent format: <code>uuuu-MM-dd HH:mm:ss</code>)</p></li><li><p><code>LocalTime</code> example: <code>10:15:30.000000</code> (equivalent format: <code>HH:mm:ss.n</code>)</p></li><li><p><code>Time</code> example: <code>10:15:30.000000Z</code> (equivalent format: <code>HH:mm:ss.nxxxz</code>)</p></li></ul></div></div></td></tr><tr><td><p><code>locale</code></p></td><td><div><div><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. When you pass a translatable format, such as <code>eeee</code> and <code>MMMM</code>, a <code>locale</code> (such as <code>"ES</code>) transforms the corresponding numeric values to a localized string.</p></div></div></td></tr></tbody></table>

### Example

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

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
   aDate: toString(|2003-10-01|, "uuuu/MM/dd"),
   aDateTime: toString(|2018-09-17T22:13:00-03:00|),
   aLocalTime: toString(|23:57:59|, "HH-mm-ss"),
   aLocalDateTime : toString(|2015-10-01T23:57:59|),
   aLocalDateTimeFormatted: toString(|2003-10-01T23:57:59|, "uuuu-MM-dd HH:mm:ss a"),
   aLocalDateTimeFormattedAndLocalizedSpain: toString(|2003-01-01T23:57:59|, "eeee, dd MMMM, uuuu HH:mm:ss a", "ES"),
   aTime: typeOf(|22:10:18Z|),
   aTimeZone: toString(|-03:00|)
}
```

#### Output

```Json
{
  "aDate": "2003/10/01",
  "aDateTime": "2018-09-17T22:13:00-03:00",
  "aLocalTime": "23-57-59",
  "aLocalDateTime": "2015-10-01T23:57:59",
  "aLocalDateTimeFormatted": "2003-10-01 23:57:59 PM",
  "aLocalDateTimeFormattedAndLocalizedSpain": "miércoles, 01 enero, 2003 23:57:59 p. m.",
  "aTime": "Time",
  "aTimeZone": "-03:00"
}
```

## toString(binary: Binary, encoding: String): String

A variant of `toString` that transforms a `Binary` value into a `String` value with the specified
encoding.

_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>binary</code></p></td><td><p>The <code>Binary</code> value to coerce to a <code>String</code> value.</p></td></tr><tr><td><p><code>encoding</code></p></td><td><p>The encoding to apply to the <code>String</code> value. Accepts encodings that are supported by your JDK. For example, <code>encoding</code> accepts Java canonical names and aliases for the basic and extended encoding sets in Oracle JDK 8 and JDK 11.</p></td></tr></tbody></table>

### Example

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

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
var binaryData= "DW Test" as Binary {encoding: "UTF-32"}
output application/json
---
{
  a: toString(binaryData, "UTF-32"),
}
```

#### Output

```Json
{
  "a": "DW Test"
}
```

## toString(data: TimeZone | Uri | Boolean | Period | Regex | Key): String

A variant of `toString` that transforms a `TimeZone`, `Uri`, `Boolean`, `Period`, `Regex`, or `Key`
value into a string.

_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>data</code></p></td><td><p>The <code>TimeZone</code>, <code>Uri</code>, <code>Boolean</code>, <code>Period</code>, <code>Regex</code>, or <code>Key</code> value to coerce to a <code>String</code> value.</p></td></tr></tbody></table>

### Example

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

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
  transformTimeZone: toString(|Z|),
  transformBoolean: toString(true),
  transformPeriod: toString(|P1D|),
  transformRegex: toString(/a-Z/),
  transformPeriod: toString(|PT8M10S|),
  transformUri: toString("https://docs.mulesoft.com/" as Uri)
}  ++
{ transformKey : toString((keysOf({ "aKeyToString" : "aValue"})[0])) }
```

#### Output

```Json
{
  "transformTimeZone": "Z",
  "transformBoolean": "true",
  "transformPeriod": "P1D",
  "transformRegex": "a-Z",
  "transformPeriod": "PT8M10S",
  "transformUri": "https://docs.mulesoft.com/",
  "transformKey": "aKeyToString"
}
```

## toString(arr: Array<String>): String

A variant of `toString` that joins an `Array` of characters into a single `String` value.

_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>arr</code></p></td><td><p>The <code>Array</code> of characters to transform into a <code>String</code> value.</p></td></tr></tbody></table>

### Example

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

#### Source

```DataWeave
%dw 2.0
import * from dw::util::Coercions
output application/json
---
{
  a: toString([]),
  b: toString(["h", "o", "l", "a"])
}
```

#### Output

```Json
{
  "a": "",
  "b": "hola"
}
```
