Dart:
Rounding numbers

How to:

Dart provides native methods in its core num type for rounding operations. Here, we’ll explore methods like round(), floor(), ceil(), and how to round to a specific number of decimal places.

Rounding to the nearest whole number:

var number = 3.56;
print(number.round()); // Outputs: 4

Rounding down:

print(number.floor()); // Outputs: 3

Rounding up:

print(number.ceil()); // Outputs: 4

Rounding to a specific number of decimal places:

To round to a specific number of decimal places, we can use the toStringAsFixed() method, which returns a string, or use a combination of pow from dart:math for a numerical result.

import 'dart:math';

var number = 3.56789;
String roundedString = number.toStringAsFixed(2); // For display purposes
print(roundedString); // Outputs: 3.57

double roundedNumber = double.parse(roundedString);
print(roundedNumber); // Outputs: 3.57

// Alternatively, for a numerical result:
double roundedToDecimal = (number * pow(10, 2)).round().toDouble() / pow(10, 2);
print(roundedToDecimal); // Outputs: 3.57

While Dart’s core library covers most rounding needs effectively, for more complex mathematical operations or precise rounding requirements, libraries such as decimal can be useful. The decimal library provides an easy way to work with decimal numbers without losing precision, which is especially handy for financial calculations, but for simple rounding methods as shown, the Dart core functionality is typically sufficient.