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.DOLLARDE
author
Ben Vogt <[email protected]>
date
2017-02-20 21:59:18
stats
2 file(s) changed, 51 insertions(+), 2 deletions(-)
files
src/RawFormulas/RawFormulas.ts
tests/FormulasTest.ts
 1diff --git a/src/RawFormulas/RawFormulas.ts b/src/RawFormulas/RawFormulas.ts
 2index 7c6f00b..39dbe59 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 DOLLARDE = Formula["DOLLARDE"];
10 var DOLLARFR = Formula["DOLLARFR"];
11 var EDATE = function (start_date: Date, months) {
12   return moment(start_date).add(months, 'months').toDate();
13@@ -155,6 +154,31 @@ var DOLLAR = function (...values) : number {
14 };
15 
16 
17+/**
18+ * Converts a price quotation given as a decimal fraction into a decimal value.
19+ * @param values[0] fractional_price - The price quotation given using fractional decimal conventions.
20+ * @param values[1] unit - The units of the fraction, e.g. 8 for 1/8ths or 32 for 1/32nds.
21+ * @returns {number} decimal value.
22+ * @constructor
23+ */
24+var DOLLARDE = function (...values) : number {
25+  ArgsChecker.checkLength(values, 2);
26+  var dollar = TypeCaster.firstValueAsNumber(values[0]);
27+  var fraction = Math.floor(TypeCaster.firstValueAsNumber(values[1]));
28+  if (fraction === 0) {
29+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Function DOLLARDE parameter 2 cannot be zero.");
30+  }
31+  var result = parseInt(dollar.toString(), 10);
32+  result += (dollar % 1) * Math.pow(10, Math.ceil(Math.log(fraction) / Math.LN10)) / fraction;
33+  var power = Math.pow(10, Math.ceil(Math.log(fraction) / Math.LN2) + 1);
34+  if (power === 0) {
35+    throw new CellError(ERRORS.DIV_ZERO_ERROR, "Evaluation of function DOLLARDE caused a divide by zero error.");
36+  }
37+  result = Math.round(result * power) / power;
38+  return result;
39+};
40+
41+
42 
43 /**
44  * Returns the number of ways to choose some number of objects from a pool of a given size of objects.
45diff --git a/tests/FormulasTest.ts b/tests/FormulasTest.ts
46index d71dc7c..a059f0b 100644
47--- a/tests/FormulasTest.ts
48+++ b/tests/FormulasTest.ts
49@@ -942,7 +942,33 @@ catchAndAssertEquals(function() {
50 }, ERRORS.NA_ERROR);
51 
52 
53+// Test  DOLLARDE
54+assertEquals(DOLLARDE(0, 32), 0);
55 assertEquals(DOLLARDE(100.1, 32), 100.3125);
56+assertEquals(DOLLARDE(100.1, 32.9999), 100.3125);
57+assertEquals(DOLLARDE("100.1", [32, "str"]), 100.3125);
58+catchAndAssertEquals(function() {
59+  DOLLARDE(100, []);
60+}, ERRORS.REF_ERROR);
61+catchAndAssertEquals(function() {
62+  DOLLARDE(100, "str");
63+}, ERRORS.VALUE_ERROR);
64+catchAndAssertEquals(function() {
65+  DOLLARDE(100, 0);
66+}, ERRORS.DIV_ZERO_ERROR);
67+catchAndAssertEquals(function() {
68+  DOLLARDE(100, 0.99);
69+}, ERRORS.DIV_ZERO_ERROR);
70+catchAndAssertEquals(function() {
71+  DOLLARDE();
72+}, ERRORS.NA_ERROR);
73+catchAndAssertEquals(function() {
74+  DOLLARDE(3.1);
75+}, ERRORS.NA_ERROR);
76+catchAndAssertEquals(function() {
77+  DOLLARDE(3.1, 32, 22);
78+}, ERRORS.NA_ERROR);
79+
80 
81 assertEquals(DOLLARFR(100.1, 32), 100.032);
82