Commit 1710e19b authored by Mathieu Giraud's avatar Mathieu Giraud
Browse files

js/tools.js, tests: nice_{ceil,floor} with force_pow10

See #2699.
parent 61571169
...@@ -296,12 +296,12 @@ function floor_pow10(x) ...@@ -296,12 +296,12 @@ function floor_pow10(x)
* nice_ceil(23.4) -> 30 * nice_ceil(23.4) -> 30
**/ **/
function nice_ceil(x) function nice_ceil(x, force_pow10)
{ {
if (x <= 0) return x if (x <= 0) return x
try { try {
var floor_power10 = floor_pow10(x) var floor_power10 = (typeof force_pow10 == 'undefined') ? floor_pow10(x) : force_pow10
var xx = x / floor_power10 var xx = x / floor_power10
return (xx == 1 ? 1 : xx <= 1.5 ? 1.5 : Math.ceil(xx)) * floor_power10 return (xx == 1 ? 1 : xx <= 1.5 ? 1.5 : Math.ceil(xx)) * floor_power10
...@@ -317,14 +317,16 @@ function nice_ceil(x) ...@@ -317,14 +317,16 @@ function nice_ceil(x)
* Give a nice decimal number under the given number * Give a nice decimal number under the given number
* nice_floor(0.14) -> 0.1 * nice_floor(0.14) -> 0.1
* nice_floor(23.4) -> 20 * nice_floor(23.4) -> 20
* nice_floor(23.4, 1) -> 23
* nice_floor(23.4, 100) -> 0
**/ **/
function nice_floor(x) function nice_floor(x, force_pow10)
{ {
if (x <= 0) return x if (x <= 0) return x
try { try {
var floor_power10 = floor_pow10(x) var floor_power10 = (typeof force_pow10 == 'undefined') ? floor_pow10(x) : force_pow10
return Math.floor(x / floor_power10) * floor_power10 return Math.floor(x / floor_power10) * floor_power10
} }
catch(e) { catch(e) {
......
...@@ -46,6 +46,10 @@ QUnit.test("test rounding functions", function(assert) { ...@@ -46,6 +46,10 @@ QUnit.test("test rounding functions", function(assert) {
assert.equal(nice_floor(100), 100, "rounding 100"); assert.equal(nice_floor(100), 100, "rounding 100");
assert.equal(nice_floor(451), 400, "rounding 451"); assert.equal(nice_floor(451), 400, "rounding 451");
assert.equal(nice_floor(23.4), 20, "rounding 23.4");
assert.equal(nice_floor(23.4, 1), 23, "rounding 23.4 (base 1)");
assert.equal(nice_floor(23.4, 100), 0, "rounding 23.4 (base 100)");
assert.equal(nice_number_digits(42, 1), 0, "nice_number_digits 42"); assert.equal(nice_number_digits(42, 1), 0, "nice_number_digits 42");
assert.equal(nice_number_digits(45.1, 2), 0, "nice_number_digits 45.1"); assert.equal(nice_number_digits(45.1, 2), 0, "nice_number_digits 45.1");
assert.equal(nice_number_digits(4.51, 2), 1, "nice_number_digits 4.51"); assert.equal(nice_number_digits(4.51, 2), 1, "nice_number_digits 4.51");
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment