How do you round off float values in Ballerina?

北城以北 提交于 2019-12-11 01:36:19

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!