2

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: "##.##" }
}
Carlos
  • 139
  • 2
  • 12

3 Answers3

2

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.

Ferdy Pruis
  • 1,073
  • 8
  • 9
0

I think thats more for formatting strings. Try this for decimal points:

{

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

}
Ryan Carter
  • 11,441
  • 2
  • 20
  • 27
  • That works for all output types application/json etc. Just not application/java. Im guessing that what you are using? – Ryan Carter Oct 22 '15 at 13:41
0

I'm using:

%output application/json
%type currency = :string { format: "###,##0.00"} 
%function toLocalCurrency (currency) currency replace "." with "#" replace "," with "." replace "#" with ","
---
{
    usCurrencyWithOneDecimal: 900000.1 as :currency,
    brCurrency: toLocalCurrency(900000.1 as :currency),
    usCurrencyWithTwoDecimal: 900000.12 as :currency,
    usCurrencyWithThreeDecimal: 900000.124 as :currency,
    usCurrencyWithThreeDecimalRounding: 900000.125 as :currency,
    usCurrencyZero: 0 as :currency
}

The output is:

{
  "usCurrencyWithOneDecimal": "900,000.10",
  "brCurrency": "900.000,10",
  "usCurrencyWithTwoDecimal": "900,000.12",
  "usCurrencyWithThreeDecimal": "900,000.12",
  "usCurrencyWithThreeDecimalRounding": "900,000.12",
  "usCurrencyZero": "0.00"
}