Loading

MuleSoft DataWeave Error "Cannot coerce String (0,000000) to Number" Formatting a Decimal Number To String

Publiceringsdatum: Jan 9, 2026
Beskrivning

In MuleSoft DataWeave, when converting a decimal number to a string using the format "(0.00)" as String {format: "0.0000000"}. Instead of getting a decimal separated with a decimal point "0.0000000", you get a decimal separated with a comma "0,0000000". This would subsequently impact follow-on flows when this String value is converted back to a number and with the comma separator, as shown in the below example.

ERROR 2023-04-05 09:13:56,739 [[MuleRuntime].uber.01: [testdwissue].testdwissueFlow.CPU_INTENSIVE @da052a5] [processor: ; event: 4f080140-d381-11ed-ab5f-fe5c682c08df] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
********************************************************************************
Message               : "org.mule.weave.v2.exception.InvalidNumberException: Cannot coerce String (0,372970) to Number

3| 	(nsv * rate) as String {format: "0.000000"} as Number
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Trace:
  at anonymous::calcNSV (line: 3, column: 3)
  at anonymous::main (line: 5, column: 1)
" evaluating expression: "output application/json
fun calcNSV(nsv, rate) =  
	(nsv * rate) as String {format: "0.000000"} as Number
---
calcNSV(12.206284506290 default 0, 0.03055556 default 0)".
Element               : testdwissueFlow/processors/0 @ testdwissue:testdwissue.xml:13 (Transform Message)
Element DSL           : <ee:transform doc:name="Transform Message" doc:id="f8fcc7f6-2699-462a-8136-d3d5b7fe0912">
<ee:message>
<ee:set-payload><![CDATA[
output application/json
fun calcNSV(nsv, rate) =  
	(nsv * rate) as String {format: "0.000000"} as Number
---
calcNSV(12.206284506290 default 0, 0.03055556 default 0)
]]></ee:set-payload>
</ee:message>
</ee:transform>
Error type            : MULE:EXPRESSION
FlowStack             : at testdwissueFlow(testdwissueFlow/processors/0 @ testdwissue:testdwissue.xml:13 (Transform Message))
Payload               : null
Payload Type          : null
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.runtime.api.el.ExpressionExecutionException: org.mule.weave.v2.exception.InvalidNumberException: Cannot coerce String (0,372970) to Number

3| 	(nsv * rate) as String {format: "0.000000"} as Number

      screenshot

Lösning

When receiving the error in MuleSoft's DataWeave "Cannot coerce String to Number", a locale is not specified if the application is being run on a machine that uses a regional setup and the "," is used as a decimal separator. 

SOLUTION

In DataWeave, to format the decimal numbers with a "." separator as in the U.S., you will need to specify the locale as detailed in this document.

For example, this script will output "0,3400":
- 0.34 as String {format: "0.0000", locale: "ES"}

While this script will output "0.3400":
- 0.34 as String {format: "0.0000", locale: "US"}

Knowledge-artikelnummer

001123351

 
Laddar
Salesforce Help | Article