encodeAmount function

String encodeAmount(
  1. double amount
)

Formats a BIP21 amount as plain decimal BTC.

double.toString() switches to scientific notation below 1e-6 — one satoshi renders as 1e-8 — which no BIP21 grammar accepts: it allows an optional X/x exponent, never e. Format at satoshi precision and trim the trailing zeros so canonical amounts such as 20.3 are unchanged.

See test/double_test.dart, which already documented this trap.

Implementation

String encodeAmount(double amount) {
  var output = amount.toStringAsFixed(8);
  if (!output.contains('.')) return output;
  output = output.replaceAll(RegExp(r'0+$'), '');
  if (output.endsWith('.')) output = output.substring(0, output.length - 1);
  return output;
}