在 BallerinaLang 中,如何将浮点值四舍五入到指定的小数位数?
Ballerina尚未提供浮动收尾的具体方法。但使用数学:圆 https://ballerina.io/learn/api-docs/ballerina/math.html#round现有的数学包 https://ballerina.io/learn/api-docs/ballerina/math.html,可以进行以下操作。
import ballerina/math;
function roundFloat(float value, int decimalPlaces) returns float {
float factor = math:pow(10, decimalPlaces);
return <float> math:round(value * factor)/factor;
}
function main(string... args) {
float result = roundFloat(12.84675, 2);
}
PS:math:round函数仅将浮点数舍入为最接近的int
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)