round function
Rounds value to the nearest integer.
- Accepts
int,double, or numericString. - If
valueis already anint, it is returned unchanged. - If
valueis adouble, returns the result of.round(). - If
valueis aString, attempts to parse it as adoubleand then rounds the result. - Returns
nullifvalueisnull. - Throws Exception if
valueis not a supported type.
Example:
round(3.6); // -> 4
round("7.2"); // -> 7
round(5); // -> 5
round(null); // -> null
Implementation
int? round(dynamic value) {
if (value == null) return null;
if (value is int) return value;
if (value is double) return value.round();
if (value is String) return double.parse(value).round();
throw Exception("Invalid value '$value' for 'round' function.");
}