Flutter 的Dart语言,数字向上或者向下取整数。
例如:
6除以5想要获取到不同的值
var a = 6/5;//a=1.2
var a = 6~/5;//a=1
var num = 10/3;
//向上取整(返回double)
var upperNum = num.ceilToDouble();//4.0
//向上取整(返回int)
var upperNum = num.ceil();//4
//向下取整(返回double)
var downNum = num.truncateToDouble();//3.0
//向下取整(返回int)
var downNum = num.truncate();//3
//四舍五入取整(返回double)
var downNum = num.roundToDouble();//3.0
//四舍五入取整(返回int)
var downNum = num.round();//3
控制小数点精度
//toStringAsFixed()会进行四舍五入
var a = 10/3;
var b = a.toStringAsFixed(2);//3.33
var c = a.toStringAsFixed(5);//3.33333
网友评论