Line data Source code
1 : import 'dart:math'; 2 : 3 : import 'package:folly_fields/util/hashable.dart'; 4 : 5 : /// 6 : /// 7 : /// 8 : class Decimal with Hashable { 9 : final int precision; 10 : double doubleValue; 11 : 12 : /// 13 : /// 14 : /// 15 4 : Decimal({ 16 : required this.precision, 17 : int? intValue, 18 : double? doubleValue, 19 8 : }) : assert(precision >= 0, 'precision must be positive or zero'), 20 : assert( 21 4 : intValue == null || doubleValue == null, 22 : 'intValue or doubleValue must be null', 23 : ), 24 : doubleValue = 25 6 : doubleValue ?? (intValue ?? 0).toDouble() / pow(10, precision); 26 : 27 : /// 28 : /// 29 : /// 30 4 : int get intValue => 31 24 : int.parse((doubleValue * pow(10, precision)).toStringAsFixed(0)); 32 : 33 : /// 34 : /// 35 : /// 36 3 : @override 37 9 : String toString() => doubleValue.toStringAsFixed(precision); 38 : 39 : /// 40 : /// 41 : /// 42 3 : @override 43 12 : int get hashCode => finish(combine(precision, intValue)); 44 : 45 : /// 46 : /// 47 : /// 48 3 : @override 49 : bool operator ==(Object other) { 50 3 : if (other is Decimal) { 51 18 : return precision == other.precision && doubleValue == other.doubleValue; 52 : } 53 : 54 : return false; 55 : } 56 : }