formatCopyNumber function

String formatCopyNumber(
  1. double value
)

Prints value for copy: at most four decimals, no trailing zeros, "" for a non-finite value.

Implementation

String formatCopyNumber(double value) {
  if (!value.isFinite) return '';
  final rounded = (value * 10000).roundToDouble() / 10000;
  // -0.0 == 0, so this also stops negative zero printing as "-0".
  if (rounded == 0) return '0';
  var result = rounded.toStringAsFixed(4);
  if (result.contains('.')) {
    result = result.replaceAll(RegExp(r'0+$'), '');
    result = result.replaceAll(RegExp(r'\.$'), '');
  }
  return result;
}