1 QUnit.test("kivi.round_amount function, 0..2 places", function( assert ) {
2 assert.equal(kivi.round_amount(1.05, 2), '1.05', '1.05 @ 2');
3 assert.equal(kivi.round_amount(1.05, 1), '1.1', '1.05 @ 1');
4 assert.equal(kivi.round_amount(1.05, 0), '1', '1.05 @ 0');
6 assert.equal(kivi.round_amount(1.045, 2), '1.05', '1.045 @ 2');
7 assert.equal(kivi.round_amount(1.045, 1), '1', '1.045 @ 1');
8 assert.equal(kivi.round_amount(1.045, 0), '1', '1.045 @ 0');
10 assert.equal(kivi.round_amount(33.675, 2), '33.68', '33.675 @ 2');
11 assert.equal(kivi.round_amount(33.675, 1), '33.7', '33.675 @ 1');
12 assert.equal(kivi.round_amount(33.675, 0), '34', '33.675 @ 0');
14 assert.equal(kivi.round_amount(64.475, 2), '64.48', '64.475 @ 2');
15 assert.equal(kivi.round_amount(64.475, 1), '64.5', '64.475 @ 1');
16 assert.equal(kivi.round_amount(64.475, 0), '64', '64.475 @ 0');
18 assert.equal(kivi.round_amount(44.9 * 0.75, 2), '33.68', '44.9 * 0.75 @ 2');
19 assert.equal(kivi.round_amount(44.9 * 0.75, 1), '33.7', '44.9 * 0.75 @ 1');
20 assert.equal(kivi.round_amount(44.9 * 0.75, 0), '34', '44.9 * 0.75 @ 0');
22 assert.equal(kivi.round_amount(143.20, 2), '143.2', '143.20 @ 2');
23 assert.equal(kivi.round_amount(143.20, 1), '143.2', '143.20 @ 1');
24 assert.equal(kivi.round_amount(143.20, 0), '143', '143.20 @ 0');
26 assert.equal(kivi.round_amount(149.175, 2), '149.18', '149.175 @ 2');
27 assert.equal(kivi.round_amount(149.175, 1), '149.2', '149.175 @ 1');
28 assert.equal(kivi.round_amount(149.175, 0), '149', '149.175 @ 0');
30 assert.equal(kivi.round_amount(198.90 * 0.75, 2), '149.18', '198.90 * 0.75 @ 2');
31 assert.equal(kivi.round_amount(198.90 * 0.75, 1), '149.2', '198.90 * 0.75 @ 1');
32 assert.equal(kivi.round_amount(198.90 * 0.75, 0), '149', '198.90 * 0.75 @ 0');
35 QUnit.test("kivi.round_amount function, 0..5 places", function( assert ) {
36 assert.equal(kivi.round_amount(64.475499, 5), '64.4755', '64.475499 @ 5');
37 assert.equal(kivi.round_amount(64.475499, 4), '64.4755', '64.475499 @ 4');
38 assert.equal(kivi.round_amount(64.475499, 3), '64.475', '64.475499 @ 3');
39 assert.equal(kivi.round_amount(64.475499, 2), '64.48', '64.475499 @ 2');
40 assert.equal(kivi.round_amount(64.475499, 1), '64.5', '64.475499 @ 1');
41 assert.equal(kivi.round_amount(64.475499, 0), '64', '64.475499 @ 0');
43 assert.equal(kivi.round_amount(64.475999, 5), '64.476', '64.475999 @ 5');
44 assert.equal(kivi.round_amount(64.475999, 4), '64.476', '64.475999 @ 4');
45 assert.equal(kivi.round_amount(64.475999, 3), '64.476', '64.475999 @ 3');
46 assert.equal(kivi.round_amount(64.475999, 2), '64.48', '64.475999 @ 2');
47 assert.equal(kivi.round_amount(64.475999, 1), '64.5', '64.475999 @ 1');
48 assert.equal(kivi.round_amount(64.475999, 0), '64', '64.475999 @ 0');
51 QUnit.test("kivi.round_amount function, negative values", function( assert ) {
53 assert.equal(kivi.round_amount(-1.05, 2), '-1.05', '-1.05 @ 2');
54 assert.equal(kivi.round_amount(-1.05, 1), '-1.1', '-1.05 @ 1');
55 assert.equal(kivi.round_amount(-1.05, 0), '-1', '-1.05 @ 0');
57 assert.equal(kivi.round_amount(-1.045, 2), '-1.05', '-1.045 @ 2');
58 assert.equal(kivi.round_amount(-1.045, 1), '-1', '-1.045 @ 1');
59 assert.equal(kivi.round_amount(-1.045, 0), '-1', '-1.045 @ 0');
61 assert.equal(kivi.round_amount(-33.675, 2), '-33.68', '33.675 @ 2');
62 assert.equal(kivi.round_amount(-33.675, 1), '-33.7', '33.675 @ 1');
63 assert.equal(kivi.round_amount(-33.675, 0), '-34', '33.675 @ 0');
65 assert.equal(kivi.round_amount(-44.9 * 0.75, 2), '-33.68', '-44.9 * 0.75 @ 2');
66 assert.equal(kivi.round_amount(-44.9 * 0.75, 1), '-33.7', '-44.9 * 0.75 @ 1');
67 assert.equal(kivi.round_amount(-44.9 * 0.75, 0), '-34', '-44.9 * 0.75 @ 0');
69 assert.equal(kivi.round_amount(-149.175, 2), '-149.18', '-149.175 @ 2');
70 assert.equal(kivi.round_amount(-149.175, 1), '-149.2', '-149.175 @ 1');
71 assert.equal(kivi.round_amount(-149.175, 0), '-149', '-149.175 @ 0');
73 assert.equal(kivi.round_amount(-198.90 * 0.75, 2), '-149.18', '-198.90 * 0.75 @ 2');
74 assert.equal(kivi.round_amount(-198.90 * 0.75, 1), '-149.2', '-198.90 * 0.75 @ 1');
75 assert.equal(kivi.round_amount(-198.90 * 0.75, 0), '-149', '-198.90 * 0.75 @ 0');
78 QUnit.test("kivi.round_amount function, programmatic tests of all 0..1000", function( assert ) {
79 $([ -1, 1 ]).each(function(dummy, sign) {
80 for (var idx = 0; idx < 1000; idx++) {
81 var num = (9900000 * sign) + idx;
83 str = str.substr(0, str.length - 2) + "." + str.substr(str.length - 2, 2);
84 str = str.replace(/0+$/, '').replace(/\.$/, '');
92 assert.equal("" + kivi.round_amount(num, 2), str);
97 QUnit.test("kivi.round_amount function, up to 10 digits of Pi", function( assert ) {
98 // round to any digit we like
99 assert.equal(kivi.round_amount(Math.PI, 0), '3', "0 digits of π");
100 assert.equal(kivi.round_amount(Math.PI, 1), '3.1', "1 digit of π");
101 assert.equal(kivi.round_amount(Math.PI, 2), '3.14', "2 digits of π");
102 assert.equal(kivi.round_amount(Math.PI, 3), '3.142', "3 digits of π");
103 assert.equal(kivi.round_amount(Math.PI, 4), '3.1416', "4 digits of π");
104 assert.equal(kivi.round_amount(Math.PI, 5), '3.14159', "5 digits of π");
105 assert.equal(kivi.round_amount(Math.PI, 6), '3.141593', "6 digits of π");
106 assert.equal(kivi.round_amount(Math.PI, 7), '3.1415927', "7 digits of π");
107 assert.equal(kivi.round_amount(Math.PI, 8), '3.14159265', "8 digits of π");
108 assert.equal(kivi.round_amount(Math.PI, 9), '3.141592654', "9 digits of π");
109 assert.equal(kivi.round_amount(Math.PI, 10), '3.1415926536', "10 digits of π");
112 QUnit.test("kivi.round_amount function, other weird cases", function( assert ) {
114 assert.equal(kivi.round_amount(1.2, 200), '1.2', '1.2 @ 200');