Bugfix: Liquid asset issuance / circulating / burned amounts are missing decimal point
fixes #112
This commit is contained in:
@@ -63,6 +63,68 @@ export function calcSegwitFeeGains(tx: Transaction) {
|
||||
};
|
||||
}
|
||||
|
||||
// https://github.com/shesek/move-decimal-point
|
||||
export function moveDec(num: number, n: number) {
|
||||
let frac, int, neg, ref;
|
||||
if (n === 0) {
|
||||
return num;
|
||||
}
|
||||
ref = ('' + num).split('.'), int = ref[0], frac = ref[1];
|
||||
int || (int = '0');
|
||||
frac || (frac = '0');
|
||||
neg = (int[0] === '-' ? '-' : '');
|
||||
if (neg) {
|
||||
int = int.slice(1);
|
||||
}
|
||||
if (n > 0) {
|
||||
if (n > frac.length) {
|
||||
frac += zeros(n - frac.length);
|
||||
}
|
||||
int += frac.slice(0, n);
|
||||
frac = frac.slice(n);
|
||||
} else {
|
||||
n = n * -1;
|
||||
if (n > int.length) {
|
||||
int = (zeros(n - int.length)) + int;
|
||||
}
|
||||
frac = int.slice(n * -1) + frac;
|
||||
int = int.slice(0, n * -1);
|
||||
}
|
||||
while (int[0] === '0') {
|
||||
int = int.slice(1);
|
||||
}
|
||||
while (frac[frac.length - 1] === '0') {
|
||||
frac = frac.slice(0, -1);
|
||||
}
|
||||
return neg + (int || '0') + (frac.length ? '.' + frac : '');
|
||||
}
|
||||
|
||||
function zeros(n) {
|
||||
return new Array(n + 1).join('0');
|
||||
}
|
||||
|
||||
// Formats a number for display. Treats the number as a string to avoid rounding errors.
|
||||
export const formatNumber = (s, precision = null) => {
|
||||
let [ whole, dec ] = s.toString().split('.');
|
||||
|
||||
// divide numbers into groups of three separated with a thin space (U+202F, "NARROW NO-BREAK SPACE"),
|
||||
// but only when there are more than a total of 5 non-decimal digits.
|
||||
if (whole.length >= 5) {
|
||||
whole = whole.replace(/\B(?=(\d{3})+(?!\d))/g, '\u202F');
|
||||
}
|
||||
|
||||
if (precision != null && precision > 0) {
|
||||
if (dec == null) {
|
||||
dec = '0'.repeat(precision);
|
||||
}
|
||||
else if (dec.length < precision) {
|
||||
dec += '0'.repeat(precision - dec.length);
|
||||
}
|
||||
}
|
||||
|
||||
return whole + (dec != null ? '.' + dec : '');
|
||||
};
|
||||
|
||||
// Utilities for segwitFeeGains
|
||||
const witnessSize = (vin: Vin) => vin.witness.reduce((S, w) => S + (w.length / 2), 0);
|
||||
const scriptSigSize = (vin: Vin) => vin.scriptsig ? vin.scriptsig.length / 2 : 0;
|
||||
|
||||
Reference in New Issue
Block a user