【问题标题】:How do you round off float values in Ballerina?你如何在 Ballerina 中四舍五入浮点值?
【发布时间】:2018-07-06 13:29:30
【问题描述】:

在 BallerinaLang 中,如何将浮点值四舍五入到指定的小数位数?

【问题讨论】:

    标签: ballerina


    【解决方案1】:

    Ballerina 还没有提供具体的浮动四舍五入方法。但是使用现有math packagemath:round,可以进行以下操作。

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 2010-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多