0

I have a multi client accounting app. The preference of decimal points in a double is different for different clients. Is there any way to set the precision of double both in calculation and display globally ?

1 Answers1

0

I'm afraid there's no way to do it.

I have an app which is very similar to yours, and I have created a class for all numbers:

const int precision = 3;

class NumberEntry implements Comparable<NumberEntry> {
  final num value;

  NumberEntry(num input) : value = _round(input);

  static num _round(num input) {
    return num.parse(input.toStringAsFixed(precision));
  }

  NumberEntry operator +(NumberEntry other) => NumberEntry(value + other.value);
  NumberEntry operator -(NumberEntry other) => NumberEntry(value - other.value);
  NumberEntry operator *(NumberEntry other) => NumberEntry(value * other.value);
  NumberEntry operator /(NumberEntry other) => NumberEntry(value / other.value);
  bool operator >(NumberEntry other) => value > other.value;
  bool operator <(NumberEntry other) => value < other.value;
  bool operator >=(NumberEntry other) => value >= other.value;
  bool operator <=(NumberEntry other) => value <= other.value;

  @override
  int compareTo(NumberEntry other) {
    if (value == other.value) {
      return 0;
    }
    return value > other.value ? 1 : -1;
  }

  @override
  String toString() {
    return displayText;
  }

  String get displayText {
    return value.toStringAsFixed(precision);
  }

  String get displayTextWithSign {
    final String additionalSign = !value.isNegative ? '+' : '';
    return additionalSign + displayText;
  }

  NumberEntry abs() {
    return NumberEntry(value.abs());
  }
}

You might modify it to suit your needs.

Crizant
  • 1,571
  • 2
  • 10
  • 14