2

我在尝试将字符串解析为 double 时遇到问题,这将是一个示例代码,它返回一个整数而不是 type :double 有什么想法吗?

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

3 回答 3

2

这个,只有这个,对我有用;

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

格式似乎只在从数字转换为字符串时添加零。如果“22”已经是一个数字,则不需要第一个 :number 转换;

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

后一种数字转换使其输出为浮点数。否则你会得到一个根据主机区域设置格式化的字符串。

小心。使用时%output text/json,上面的代码在某些情况下会产生22.0而不是22.00

于 2015-11-19T14:16:59.757 回答
0

我认为更多的是格式化字符串。试试这个小数点:

{

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

}
于 2015-10-22T13:08:29.100 回答
0

我在用着:

%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
}

输出是:

{
  "usCurrencyWithOneDecimal": "900,000.10",
  "brCurrency": "900.000,10",
  "usCurrencyWithTwoDecimal": "900,000.12",
  "usCurrencyWithThreeDecimal": "900,000.12",
  "usCurrencyWithThreeDecimalRounding": "900,000.12",
  "usCurrencyZero": "0.00"
}
于 2016-12-13T19:18:14.207 回答