trunc function
dynamic
trunc(
- dynamic x
Returns the integer part of a number by removing any fractional digits.
x is the input value.
If x is NaN, returns NaN.
If x is positive, returns the floor value of x.
Otherwise, returns the ceil value of x.
Example: ``dart print(trunc(4.7)); // Output: 4 print(trunc(-4.7)); // Output: -4
Implementation
dynamic trunc(dynamic x) {
if (x is num) {
if (x.isNaN) return double.nan;
if (x > 0) return x.floorToDouble();
return x.ceilToDouble();
} else if (x is Complex) {
return x.real.truncateToDouble();
} else {
throw ArgumentError('Input should be either num or Complex');
}
}