ballerina - 你如何在 Ballerina 中四舍五入浮点值?
问题描述
在 BallerinaLang 中,如何将浮点值四舍五入到指定的小数位数?
解决方案
Ballerina 还没有提供具体的浮动四舍五入方法。但是使用math: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
推荐阅读
- regex - REGEX - 使用分隔符获取所有字符组
- python - 什么是在指定索引处对列表中的项目求和的快速、pythonic 方法?
- sql-server - SQL Server每次使用Entity Framework查询数据时是否都会生成一个执行计划
- java - Spring-boot:在 Logback 中获取 IllegalArgumentException
- javascript - 如何解决 react-virtualized (infinite loader) 中的 rowCount 问题
- video - 如何使用 hls.js 和 plyr 配置多个视频实例
- python - ModuleNotFoundError:没有名为“pyimagesearch”的模块
- php - :"Missing Image","error_user_msg":"请指定要与此广告一起投放的图片
- mysql - 如何选择支付金额的唯一客户
- python - 查找一个月内同一个人的两个地理编码之间的距离