Mule Dataweave format :number

Carlos picture Carlos · Oct 22, 2015 · Viewed 8.3k times · Source

I'm having an issue trying to parse a string to double this would be a sample code, it's returning an integer instead of type :double any ideas?

{
    "data": "22" as :number { format: "##.##" }
}

Answer

Ferdy Pruis picture Ferdy Pruis · Nov 19, 2015

This, and only this, works for me;

%dw 1.0
%output application/json
---
{
    data: "22" as :number as :string {format: ".00"} as :number
}

format only seems to add zeros when converting from a number to a string. If "22" would have already been a number you wouldn't need the first :number conversion;

data: 22 as :string {format: ".00"} as :number

The latter number conversion makes it output as a float. Otherwise you would get a string, formatted according to the hosts locale.

And beware. When using%output text/json instead, the above code will in some cases produces 22.0 instead of 22.00.