canonicalNumber static method

String canonicalNumber(
  1. num value
)

Formats value to the canonical number string used by cpt-user-attrs-v1.

The value is formatted with two fraction digits (round-half-even IEEE 754 double formatting, toFixed(2) semantics), stripping trailing zeros and any trailing decimal point (e.g. 1200.00 -> '1200', 99.50 -> '99.5').

Implementation

static String canonicalNumber(num value) {
  if (!value.isFinite) {
    throw ArgumentError.value(value, 'value', 'mrr must be a finite number');
  }
  final fixed = value.toStringAsFixed(2);
  final dot = fixed.indexOf('.');
  if (dot == -1) return fixed;
  var end = fixed.length;
  while (end > dot && fixed[end - 1] == '0') {
    end--;
  }
  if (end > dot && fixed[end - 1] == '.') {
    end--;
  }
  return fixed.substring(0, end);
}