-
-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(math): decimal round, floor and ceil
- Loading branch information
Showing
9 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Credit: | ||
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round | ||
// #Decimal_rounding | ||
|
||
"use strict"; | ||
|
||
var isValue = require("../object/is-value") | ||
, ensureInteger = require("../object/ensure-integer"); | ||
|
||
var split = String.prototype.split; | ||
|
||
module.exports = function (type) { | ||
return function (value/*, exp*/) { | ||
value = Number(value); | ||
var exp = arguments[1]; | ||
if (isValue(exp)) exp = ensureInteger(exp); | ||
if (!value) return value; | ||
if (!exp) return Math[type](value); | ||
if (!isFinite(value)) return value; | ||
|
||
// Shift | ||
var tokens = split.call(value, "e"); | ||
value = Math[type](tokens[0] + "e" + ((tokens[1] || 0) - exp)); | ||
|
||
// Shift back | ||
tokens = value.toString().split("e"); | ||
return Number(tokens[0] + "e" + (Number(tokens[1] || 0) + exp)); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
module.exports = require("./_decimal-adjust")("ceil"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
module.exports = require("./_decimal-adjust")("floor"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
"use strict"; | ||
|
||
module.exports = require("./_decimal-adjust")("round"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
// Just sanity check, as real tests are in 'round', 'ceil' and 'floor' variants | ||
a(t("round")(55.55, -1), 55.6); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
a(t(55.51, -1), 55.6); | ||
a(t(51, 1), 60); | ||
a(t(-55.59, -1), -55.5); | ||
a(t(-59, 1), -50); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
a(t(55.59, -1), 55.5); | ||
a(t(59, 1), 50); | ||
a(t(-55.51, -1), -55.6); | ||
a(t(-51, 1), -60); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"use strict"; | ||
|
||
module.exports = function (t, a) { | ||
a(t(55.55, -1), 55.6); | ||
a(t(55.549, -1), 55.5); | ||
a(t(55, 1), 60); | ||
a(t(54.9, 1), 50); | ||
a(t(-55.55, -1), -55.5); | ||
a(t(-55.551, -1), -55.6); | ||
a(t(-55, 1), -50); | ||
a(t(-55.1, 1), -60); | ||
a(t(1.005, -2), 1.01); | ||
a(t(-1.005, -2), -1.0); | ||
}; |