我正在将存储在有效负载中的字符串“0.0000000”转换为 Data Weave 语言 1.0 中的数字
我尝试payload.Amount as :number
%dw 1.0
%output application/json
---
{
Amount: payload.Amount as :number when payload.Amount != null
}
我期望最终输出为0.0000000,但我得到0E-7
最佳答案
我认为你的期望是不正确的。根据RFC 4627,0E-7 是一个完全有效的 JSON 数字。 。任何正确的解析器都应该正确处理这个有效数字。与其他语言一样,数字在 JSON 中没有格式属性。只有当它们与字符串相互转换时,才能应用格式。由于这些原因,您无法告诉 DataWeave 使用特定的格式。
现在,如果您遇到导致 0E-7 出现问题的特定问题,请更新说明以添加更多详细信息。
如果设置固定的小数位数可以的话,另一种可能有效的替代方法是采用 https://help.mulesoft.com/s/article/How-to-force-DataWeave-to-return-Long-number 中描述的方法。
示例
%dw 1.0
%output application/json
%function withZeroes(x) x as :string { format: "#.####" } as :number
---
{
Amount: withZeroes(0E-7),
Amount2: withZeroes(0.000),
Amount3: withZeroes(0.0001),
Amount4: withZeroes(0.00001)
}
输出:
{
"Amount": 0,
"Amount2": 0,
"Amount3": 0.0001,
"Amount4": 0
}
关于java - 在 DataWeave 1.0 中将字符串转换为数字时如何避免指数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57986373/