如何在 Azure 逻辑应用程序中将浮点数四舍五入为 2 位小数?

How to round off float number to 2 decimal numbers in azure logic apps?

在 azure logic 应用程序中,我将千克转换为磅,我需要将该结果四舍五入为两位小数。

Expression : mul(float(variables('total_weight')) , 2.20462262185)

Result :  1.102311310925
Expected Result : 1.10

目前,Logic Apps 中还没有通用的舍入解决方案。但是,您需要构建特定于您的数据的解决方案。

这里有一种方法,您可以将小数点舍入为两位以供显示。

if(contains('56789', substring(string(variables('math')),4,1)),substring(string(add(variables('math'),0.01)),0,4),substring(string(variables('math')),0,4))

这种方式将return字符串格式,你可以将float()添加到return浮点数。我用 2.204622621852.20562262185.

测试了两次

我对数字进行四舍五入的解决方法是使用 formatNumber() function with fixed-point format。所以你的代码看起来像这样:

formatNumber(mul(float(variables('total_weight')) , 2.20462262185), 'F2')

最后一个参数中的格式字符串 - 'F2' - 其中 2 指定小数位。两位小数是定点格式的默认值,因此您也可以只使用 'F'.

我可以看到您在另一个对象变量的 属性 上使用它,所以就像 George 提到的那样,您需要将结果包装在 float() 函数中:

float(formatNumber(mul(float(variables('total_weight')) , 2.20462262185), 'F2'))