mirror of
https://github.com/Such-Software/hash-wallet.git
synced 2026-10-28 06:28:41 +01:00
Upstream reformatted its whole tree in 16ddc0837. Matching it removes
formatting from the conflict surface of this and every later sync.
51 lines
1.2 KiB
Dart
51 lines
1.2 KiB
Dart
import 'package:cw_core/parse_fixed.dart';
|
|
|
|
String formatFixed(BigInt value, int? decimals, {int? fractionalDigits, bool trimZeros = true}) {
|
|
decimals ??= 0;
|
|
fractionalDigits ??= decimals;
|
|
|
|
final multiplier = getMultiplier(decimals);
|
|
// Make sure wei is a big number (convert as necessary)
|
|
final negative = value.isNegative;
|
|
if (negative) {
|
|
value = -value;
|
|
}
|
|
|
|
var fraction =
|
|
value.modPow(BigInt.one, BigInt.parse(multiplier)).toString().padLeft(decimals, "0");
|
|
|
|
if (fractionalDigits < 0) {
|
|
fractionalDigits = 0;
|
|
}
|
|
if (fractionalDigits > decimals) {
|
|
fractionalDigits = decimals;
|
|
}
|
|
fraction = fraction.substring(0, fractionalDigits);
|
|
|
|
if (trimZeros) {
|
|
fraction = removeTrailing("0", fraction);
|
|
}
|
|
|
|
final whole = value ~/ BigInt.parse(multiplier);
|
|
|
|
final valString = fraction.isEmpty ? "$whole" : "$whole.$fraction";
|
|
|
|
if (negative) {
|
|
return "-$valString";
|
|
}
|
|
|
|
return valString;
|
|
}
|
|
|
|
String removeTrailing(String pattern, String from) {
|
|
if (pattern.isEmpty) {
|
|
return from;
|
|
}
|
|
|
|
var i = from.length;
|
|
while (i > 0 && from.startsWith(pattern, i - pattern.length)) {
|
|
i -= pattern.length;
|
|
}
|
|
return from.substring(0, i);
|
|
}
|