everyone , i want to know anyone create function or package like _ceil
or _round()
same as lodash JavaScript utility library ? i need it , thank in advance
reference link : lodash
everyone , i want to know anyone create function or package like _ceil
or _round()
same as lodash JavaScript utility library ? i need it , thank in advance
reference link : lodash
As I explained in comments, what you want doesn't make sense for IEEE-754 floating-point numbers. That is, you can't have a specific precision in base-10 using a system that uses base-2. As a basic example, the decimal number 0.3 cannot be exactly represented in binary floating-point. (For more details, see Is floating point math broken?)
package:decimal
provides operations for working with arbitrary precision base-10 numbers, so you can use that to pretty easily create your own implementation:
import 'package:decimal/decimal.dart';
Decimal _multiplier(int precision) => Decimal.fromInt(10).pow(precision.abs());
Decimal ceil(Decimal n, [int precision = 0]) {
var multiplier = _multiplier(precision);
return (n * multiplier).ceil() / multiplier;
}
void main() {
print(ceil(Decimal.parse('4.006'))); // Prints: 5
print(ceil(Decimal.parse('6.004'), 2)); // Prints: 6.01
print(ceil(Decimal.parse('6040'), -2)); // Prints: 6100
print(ceil(Decimal.parse('0.1') + Decimal.parse('0.2'), 1)); // Prints: 0.3
}
Implementing equivalent functions for floor
and round
should be similar.