问题
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: "##.##" }
}
回答1:
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.
回答2:
I think thats more for formatting strings. Try this for decimal points:
{
"data": "22" as :number {format: ".00"}
}
回答3:
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"
}
来源:https://stackoverflow.com/questions/33281278/mule-dataweave-format-number