aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-11-16 10:29:07 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-11-16 10:29:07 +0100
commit82e401a77303fca1dc977ee3662d7364bb946ff3 (patch)
tree0a692769cccf459bed1fcd5f0b76cf0e70fdf2db /src
parent665c1d80726ce2ffdf0020de9c4dd293ccbee849 (diff)
make fractional base a constant
Diffstat (limited to 'src')
-rw-r--r--src/helpers.ts6
-rw-r--r--src/renderHtml.tsx4
-rw-r--r--src/types.ts20
3 files changed, 16 insertions, 14 deletions
diff --git a/src/helpers.ts b/src/helpers.ts
index 2e7a701c3..d9b62aa22 100644
--- a/src/helpers.ts
+++ b/src/helpers.ts
@@ -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]
}
}
diff --git a/src/renderHtml.tsx b/src/renderHtml.tsx
index 940d5c425..fe6646196 100644
--- a/src/renderHtml.tsx
+++ b/src/renderHtml.tsx
@@ -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}`;
}
diff --git a/src/types.ts b/src/types.ts
index eb4a29788..b04453767 100644
--- a/src/types.ts
+++ b/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;