问题
In BallerinaLang, how do you round off float values to a specified number of decimal places?
回答1:
Ballerina has not provided specific method yet for float round off. But using math:round of existing math package, following can be done.
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 function only rounds a floating point number to the nearest int
来源:https://stackoverflow.com/questions/51211525/how-do-you-round-off-float-values-in-ballerina