Formatting 在DataWeave中格式化时截断数字
在格式化数字时,有没有更短的方法来截断它?看起来默认行为是舍入。现在我有了这个:Formatting 在DataWeave中格式化时截断数字,formatting,mule,dataweave,Formatting,Mule,Dataweave,在格式化数字时,有没有更短的方法来截断它?看起来默认行为是舍入。现在我有了这个: var val = 123.129 --- { def: val as String {format: "0.00"}, truncate: (if(val > 0) (floor(val * 100)/100) else (ceil(val * 100)/100) ) as String {format: "0.
var val = 123.129
---
{
def: val as String {format: "0.00"},
truncate:
(if(val > 0) (floor(val * 100)/100)
else (ceil(val * 100)/100)
) as String {format: "0.00"}
}
输出是
{
"def": "123.13",
"truncate": "123.12"
}
您可以在类型转换中定义圆形模式:
down: 123.129 as String {format: "0.00", roundMode:"DOWN" }
模式基于的名称
另一种选择是执行您所做的操作,但将其封装到可重用函数中:
fun truncate(n,p) = do {
var power = 10 pow p
---
if (n>0) floor(n * power)/power
else ceil(n * power)/power
}
我找到了一个使用roundMode的受支持方法。答案已更新。