Dataweave 总是输出 JSON 有两位小数的数字

Dataweave Always output JSON number with two decimal places

我通过 Dataweave 中的 xml 收到以下号码。

<b:AmountValue>180.90</b:AmountValue>

我正在尝试使用 Dataweave 输出相应的 json 数字,小数点后两位(代表货币)。

这是我的 Dataweave 表达式的样子...

amount: $.AmountValue as :number as :string {format: ".00"} as :number

输出 json 丢失尾随零。

i.e. "amount":180.9 

如何将我的 Dataweave 表达式更改为始终保留两位小数?

或者我误解了 json 数字类型?

谢谢

如果你可以接受 amount 是一个字符串,那么远程处理最后一个 as :number

amount: $.AmountValue as :number as :string {format: ".00"}

如果你只输入 amount: $.AmountValue as :number

会发生什么

我在 anypoint studio 的预览中尝试了这个,没有看到丢失尾随 0。而且我使用的是最新版本。

我也测试了这个并且它有效。

如果你试试这个: 8.2 as :string {format: "0.00"} as :number

然后添加额外的小数位。

如果你输出到 application/json 或 application/XML 你会看到小数点后两位打印出来 8.20。如果你输出到application/java,那么它就是一个double,你将不得不决定如何打印出这个double。

如果您尝试 8.2 as :number,那么它会打印为 8.2

如前所述,从 :string 到 :number 的强制转换是删除尾随 0 的原因。Dataweave 支持使用 octothorp 指定的 :number 格式。

下面的代码片段将产生您需要的两位小数。

 amount: $.AmountValue as  :number { format: "#.##"}