aboutsummaryrefslogtreecommitdiff
path: root/src/amounts.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/amounts.ts')
-rw-r--r--src/amounts.ts23
1 files changed, 22 insertions, 1 deletions
diff --git a/src/amounts.ts b/src/amounts.ts
index 1aefe07e1..1ab00f81d 100644
--- a/src/amounts.ts
+++ b/src/amounts.ts
@@ -31,6 +31,13 @@ import { Checkable } from "./checkable";
*/
export const fractionalBase = 1e8;
+/**
+ * How many digits behind the comma are required to represent the
+ * fractional value in human readable decimal format? Must match
+ * lg(fractionalBase)
+ */
+export const fractionalLength = 8;
+
/**
* Non-negative financial amount. Fractional values are expressed as multiples
@@ -282,7 +289,21 @@ export function fromFloat(floatVal: number, currency: string) {
* also used in JSON formats.
*/
export function toString(a: AmountJson) {
- return `${a.currency}:${a.value + (a.fraction / fractionalBase)}`;
+ let s = a.value.toString()
+
+ if (a.fraction) {
+ s = s + ".";
+ let n = a.fraction;
+ for (let i = 0; i < fractionalLength; i++) {
+ if (!n) {
+ break;
+ }
+ s = s + Math.floor(n / fractionalBase * 10).toString();
+ n = (n * 10) % fractionalBase;
+ }
+ }
+
+ return `${a.currency}:${s}`;
}