make fractional base a constant
This commit is contained in:
parent
665c1d8072
commit
82e401a773
@ -23,7 +23,7 @@
|
||||
|
||||
/// <reference path="../decl/urijs/URIjs.d.ts" />
|
||||
|
||||
import {AmountJson} from "./types";
|
||||
import {AmountJson, Amounts} from "./types";
|
||||
import URI = uri.URI;
|
||||
|
||||
export function substituteFulfillmentUrl(url: string, vars: any) {
|
||||
@ -34,7 +34,7 @@ export function substituteFulfillmentUrl(url: string, vars: any) {
|
||||
|
||||
|
||||
export function amountToPretty(amount: AmountJson): string {
|
||||
let x = amount.value + amount.fraction / 1e6;
|
||||
let x = amount.value + amount.fraction / Amounts.fractionalBase;
|
||||
return `${x} ${amount.currency}`;
|
||||
}
|
||||
|
||||
@ -63,7 +63,7 @@ export function parsePrettyAmount(pretty: string): AmountJson|undefined {
|
||||
}
|
||||
return {
|
||||
value: parseInt(res[1], 10),
|
||||
fraction: res[2] ? (parseFloat(`0.${res[2]}`) * 1e-6) : 0,
|
||||
fraction: res[2] ? (parseFloat(`0.${res[2]}`) / Amounts.fractionalBase) : 0,
|
||||
currency: res[3]
|
||||
}
|
||||
}
|
||||
|
@ -21,10 +21,10 @@
|
||||
*/
|
||||
|
||||
|
||||
import {AmountJson, Contract} from "./types";
|
||||
import {AmountJson, Contract, Amounts} from "./types";
|
||||
|
||||
export function prettyAmount(amount: AmountJson) {
|
||||
let v = amount.value + amount.fraction / 1e6;
|
||||
let v = amount.value + amount.fraction / Amounts.fractionalBase;
|
||||
return `${v.toFixed(2)} ${amount.currency}`;
|
||||
}
|
||||
|
||||
|
20
src/types.ts
20
src/types.ts
@ -509,6 +509,8 @@ export type PayCoinInfo = Array<{ updatedCoin: CoinRecord, sig: CoinPaySig }>;
|
||||
|
||||
|
||||
export namespace Amounts {
|
||||
export const fractionalBase = 1e8;
|
||||
|
||||
export interface Result {
|
||||
amount: AmountJson;
|
||||
// Was there an over-/underflow?
|
||||
@ -533,18 +535,18 @@ export namespace Amounts {
|
||||
|
||||
export function add(first: AmountJson, ...rest: AmountJson[]): Result {
|
||||
let currency = first.currency;
|
||||
let value = first.value + Math.floor(first.fraction / 1e6);
|
||||
let value = first.value + Math.floor(first.fraction / fractionalBase);
|
||||
if (value > Number.MAX_SAFE_INTEGER) {
|
||||
return { amount: getMaxAmount(currency), saturated: true };
|
||||
}
|
||||
let fraction = first.fraction % 1e6;
|
||||
let fraction = first.fraction % fractionalBase;
|
||||
for (let x of rest) {
|
||||
if (x.currency !== currency) {
|
||||
throw Error(`Mismatched currency: ${x.currency} and ${currency}`);
|
||||
}
|
||||
|
||||
value = value + x.value + Math.floor((fraction + x.fraction) / 1e6);
|
||||
fraction = (fraction + x.fraction) % 1e6;
|
||||
value = value + x.value + Math.floor((fraction + x.fraction) / fractionalBase);
|
||||
fraction = (fraction + x.fraction) % fractionalBase;
|
||||
if (value > Number.MAX_SAFE_INTEGER) {
|
||||
return { amount: getMaxAmount(currency), saturated: true };
|
||||
}
|
||||
@ -567,7 +569,7 @@ export namespace Amounts {
|
||||
return { amount: { currency, value: 0, fraction: 0 }, saturated: true };
|
||||
}
|
||||
value--;
|
||||
fraction += 1e6;
|
||||
fraction += fractionalBase;
|
||||
}
|
||||
console.assert(fraction >= b.fraction);
|
||||
fraction -= b.fraction;
|
||||
@ -584,10 +586,10 @@ export namespace Amounts {
|
||||
if (a.currency !== b.currency) {
|
||||
throw Error(`Mismatched currency: ${a.currency} and ${b.currency}`);
|
||||
}
|
||||
let av = a.value + Math.floor(a.fraction / 1e6);
|
||||
let af = a.fraction % 1e6;
|
||||
let bv = b.value + Math.floor(b.fraction / 1e6);
|
||||
let bf = b.fraction % 1e6;
|
||||
let av = a.value + Math.floor(a.fraction / fractionalBase);
|
||||
let af = a.fraction % fractionalBase;
|
||||
let bv = b.value + Math.floor(b.fraction / fractionalBase);
|
||||
let bf = b.fraction % fractionalBase;
|
||||
switch (true) {
|
||||
case av < bv:
|
||||
return -1;
|
||||
|
Loading…
Reference in New Issue
Block a user