2

In BallerinaLang, how do you round off float values to a specified number of decimal places?

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.