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 7 : double.tryParse(doubleValue?.toStringAsFixed(precision) ?? '') ?? 26 9 : (intValue ?? 0).toDouble() / pow(10, precision); 27 : 28 : /// 29 : /// 30 : /// 31 6 : double get doubleValue => _doubleValue; 32 : 33 : /// 34 : /// 35 : /// 36 2 : set doubleValue(double value) => 37 8 : _doubleValue = double.parse(value.toStringAsFixed(precision)); 38 : 39 : /// 40 : /// 41 : /// 42 4 : int get intValue => 43 24 : int.parse((_doubleValue * pow(10, precision)).toStringAsFixed(0)); 44 : 45 : /// 46 : /// 47 : /// 48 3 : @override 49 9 : String toString() => _doubleValue.toStringAsFixed(precision); 50 : 51 : /// 52 : /// 53 : /// 54 3 : @override 55 12 : int get hashCode => finish(combine(precision, intValue)); 56 : 57 : /// 58 : /// 59 : /// 60 3 : @override 61 : bool operator ==(Object other) { 62 3 : if (other is Decimal) { 63 18 : return precision == other.precision && doubleValue == other.doubleValue; 64 : } 65 : 66 : return false; 67 : } 68 : }