Outline
When we use the numbers with the decimal point in Flutter, sometimes, we need to ceil, floor, and round the numbers. In this blog post, I’ll show you how to ceil, floor, and round the numbers in Flutter.
Ceil
To ceil a number in Flutter, we can use the ceil
function. You can use the ceil
function in the following way.
var targetNum = 3.514;
print(targetNum.ceil());
// 4
Floor
To floor a number in Flutter, we can use the floor
function. You can use the floor
function in the following way.
var targetNum = 3.514;
print(targetNum.floor());
// 3
Round
To round a number in Flutter, we can use the round
function. You can use the round
function in the following way.
var targetNum = 3.514;
print(targetNum.round());
// 4
targetNum = 3.154;
print(targetNum.round());
// 3
Fix the length with decimals
To fix the length of the decimal part, we can use the toStringAsFixed
function. You can use the toStringAsFixed
function in the following way.
var targetNum = 3.125;
print(targetNum.toStringAsFixed(2));
// 3.13
targetNum = 3.121;
print(targetNum.toStringAsFixed(2));
// 3.12
The toStringAsFixed
function returns a string with the fixed length. So, if you want to use the result to the number type, you should cast to the double type.
var targetNum = 3.125;
print(double.parse(targetNum.toStringAsFixed(2)));
// 3.13
targetNum = 3.121;
print(double.parse(targetNum.toStringAsFixed(2)));
// 3.12
Completed
Done! we’ve seen how to ceil, floor, and round the numbers in Flutter. Also, we’ve seen how to fix the length of the decimal part. If you fix the decimal part, notice the result of toStringAsFixed
function is a string.
Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!
App promotion
Deku
.Deku
created the applications with Flutter.If you have interested, please try to download them for free.