spreadsheet
typeScript/javascript spreadsheet parser, with formulas.
git clone https://git.vogt.world/spreadsheet.git
Log | Files | README.md
← Commit log
commit
message
Added Formulas.DOLLARFR
author
Ben Vogt <[email protected]>
date
2017-02-20 22:04:42
stats
2 file(s) changed, 49 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 39dbe59..cbbc5c4 100644
 3--- a/src/RawFormulas/RawFormulas.ts
 4+++ b/src/RawFormulas/RawFormulas.ts
 5@@ -117,7 +117,6 @@ var DATEVALUE = function (dateString: string) : Date {
 6 var DAY = Formula["DAY"];
 7 var DAYS = Formula["DAYS"];
 8 var DAYS360 = Formula["DAYS360"];
 9-var DOLLARFR = Formula["DOLLARFR"];
10 var EDATE = function (start_date: Date, months) {
11   return moment(start_date).add(months, 'months').toDate();
12 };
13@@ -144,6 +143,8 @@ var YEARFRAC = Formula["YEARFRAC"];
14  * TODO(cont.) with the primitive number type. At some point TS might allow me to suppress the warnings with
15  * TODO(cont.) https://github.com/Microsoft/TypeScript/issues/9448 or
16  * TODO(cont.) https://github.com/Microsoft/TypeScript/issues/11051
17+ *
18+ * TODO: Also, this does not do local-specific, as is.
19  */
20 var DOLLAR = function (...values) : number {
21   ArgsChecker.checkLengthWithin(values, 1, 2);
22@@ -179,6 +180,26 @@ var DOLLARDE = function (...values) : number {
23 };
24 
25 
26+/**
27+ * Converts a price quotation given as a decimal value into a decimal fraction.
28+ * @param values[0] decimal_price - The price quotation given as a decimal value.
29+ * @param values[1] unit - The units of the desired fraction, e.g. 8 for 1/8ths or 32 for 1/32nds
30+ * @returns {number} price quotation as decimal fraction.
31+ * @constructor
32+ */
33+var DOLLARFR = function (...values) : number {
34+  ArgsChecker.checkLength(values, 2);
35+  var dollar = TypeCaster.firstValueAsNumber(values[0]);
36+  var unit = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
37+  if (unit === 0) {
38+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Function DOLLARFR parameter 2 cannot be zero.");
39+  }
40+  var result = parseInt(dollar.toString(), 10);
41+  result += (dollar % 1) * Math.pow(10, -Math.ceil(Math.log(unit) / Math.LN10)) * unit;
42+  return result;
43+};
44+
45+
46 
47 /**
48  * Returns the number of ways to choose some number of objects from a pool of a given size of objects.
49diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
50index a059f0b..c89eceb 100644
51--- a/tests/FormulasTest.ts
52+++ b/tests/FormulasTest.ts
53@@ -970,7 +970,33 @@ catchAndAssertEquals(function() {
54 }, ERRORS.NA_ERROR);
55 
56 
57+// Test DOLLARFR
58 assertEquals(DOLLARFR(100.1, 32), 100.032);
59+assertEquals(DOLLARFR(100.1, 32), 100.032);
60+assertEquals(DOLLARFR(100.1, 32.9999), 100.032);
61+assertEquals(DOLLARFR("100.1", [32, "str"]), 100.032);
62+catchAndAssertEquals(function() {
63+  DOLLARFR(100, []);
64+}, ERRORS.REF_ERROR);
65+catchAndAssertEquals(function() {
66+  DOLLARFR(100, "str");
67+}, ERRORS.VALUE_ERROR);
68+catchAndAssertEquals(function() {
69+  DOLLARFR(100, 0);
70+}, ERRORS.DIV_ZERO_ERROR);
71+catchAndAssertEquals(function() {
72+  DOLLARFR(100, 0.99);
73+}, ERRORS.DIV_ZERO_ERROR);
74+catchAndAssertEquals(function() {
75+  DOLLARFR();
76+}, ERRORS.NA_ERROR);
77+catchAndAssertEquals(function() {
78+  DOLLARFR(3.1);
79+}, ERRORS.NA_ERROR);
80+catchAndAssertEquals(function() {
81+  DOLLARFR(3.1, 32, 22);
82+}, ERRORS.NA_ERROR);
83+
84 
85 assertEquals(AND(10), true);
86