Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Gaussian or banker's rounding feature #120

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added jasmine and qunit tests for gaussian rounding
Lakshmi-Sharma committed Nov 8, 2015
commit 599dc563187be57616060102a6b4ae720b15038e
2 changes: 1 addition & 1 deletion tests/jasmine/core/formatMoneySpec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
describe('formatMoney()', function(){

it('should work for small numbers', function(){

expect( accounting.formatMoney(123) ).toBe( '$123.00' );
expect( accounting.formatMoney(123.45) ).toBe( '$123.45' );
expect( accounting.formatMoney(12345.67) ).toBe( '$12,345.67' );
6 changes: 6 additions & 0 deletions tests/jasmine/core/formatNumberSpec.js
Original file line number Diff line number Diff line change
@@ -84,6 +84,12 @@ describe('formatNumber', function(){
expect( accounting.formatNumber(98765432.12, 4, '[', ']') ).toBe( '98[765[432]1200' );
});

it('should allow setting gaussian rounding', function(){
expect( accounting.formatNumber(98765432.105, 2, null, null, "gaussian") ).toBe( '98,765,432.10' );
expect( accounting.formatNumber(98765432.115, 2, null, null, "gaussian") ).toBe( '98,765,432.12' );
expect( accounting.formatNumber(98765432.125, 2, null, null, "gaussian") ).toBe( '98,765,432.12' );
});

it('should use default separators if null', function(){
expect( accounting.formatNumber(12345.12345, 2, null, null) ).toBe('12,345.12');
});
11 changes: 11 additions & 0 deletions tests/jasmine/core/toFixedSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe('toFixed()', function(){

it('should allow setting gaussian round', function(){

expect( accounting.toFixed(123.505, 2, "gaussian") ).toBe( '123.50' );
expect( accounting.toFixed(123.525, 2, "gaussian") ).toBe( '123.52' );
expect( accounting.toFixed(123.515, 2, "gaussian") ).toBe( '123.52' );

});

});
1 change: 1 addition & 0 deletions tests/jasmine/runner.html
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@
<script src="core/formatNumberSpec.js"></script>
<script src="core/unformatSpec.js"></script>
<script src="core/formatMoneySpec.js"></script>
<script src="core/toFixedSpec.js"></script>
</head>
<body>
<script>
24 changes: 23 additions & 1 deletion tests/qunit/methods.js
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ $(document).ready(function() {
test("accounting.toFixed()", function() {
equals(accounting.toFixed(54321, 5), "54321.00000", 'Performs basic float zero-padding');
equals(accounting.toFixed(0.615, 2), "0.62", 'Rounds 0.615 to "0.62" instead of "0.61"');
equals(accounting.toFixed(0.605, 2, "gaussian"), "0.60", 'Rounds 0.605 to "0.60" as per gaussian rounding');
});

test("accounting.formatNumber()", function() {
@@ -33,6 +34,14 @@ $(document).ready(function() {

// check rounding:
equals(accounting.formatNumber(0.615, 2), "0.62", 'Rounds 0.615 up to "0.62" with precision of 2');
equals(accounting.formatNumber(0.605, 2, null, null ,"gaussian"), "0.60", 'Rounds 0.605 up to "0.60" as per gaussian rounding');


// changing number settings to default gaussian rounding:
accounting.settings.number.rounding_type = "gaussian";
equals(accounting.formatNumber(0.605, 2), "0.60", 'Rounds 0.605 up to "0.60" as per gaussian rounding');
accounting.settings.number.rounding_type = "default";


// manually and recursively formatted arrays should have same values:
var numbers = [8008135, [1234, 5678], 1000];
@@ -48,6 +57,8 @@ $(document).ready(function() {
equals(accounting.formatMoney(-500000, "� ", 0), "� -500,000", 'negative values, custom params, works ok');
equals(accounting.formatMoney(5318008, { symbol: "GBP", format: "%v %s" }), "5,318,008.00 GBP", "`format` parameter is observed in string output");
equals(accounting.formatMoney(1000, { format: "test %v 123 %s test" }), "test 1,000.00 123 $ test", "`format` parameter is observed in string output, despite being rather strange");
equals(accounting.formatMoney(1234.605, null, null, null, null, null,"gaussian"), "$1,234.60", "gaussian rounding");


// Format param is an object:
var format = {
@@ -62,6 +73,13 @@ $(document).ready(function() {
accounting.settings.currency.format = "%s%v";
accounting.formatMoney(0, {format:""});
equals(typeof accounting.settings.currency.format, "object", "`settings.currency.format` default string value should be reformatted to an object, the first time it is used");


// changing currency settings to default gaussian rounding:
accounting.settings.currency.rounding_type = "gaussian";
equals(accounting.formatMoney(1234.605), "$1,234.60", "gaussian rounding");
accounting.settings.currency.rounding_type = "default";

});


@@ -71,6 +89,11 @@ $(document).ready(function() {
equals(accounting.formatColumn(list, "$ ", 0).toString(), (["$ 123", "$ 12,345"]).toString(), "formatColumn works as expected");


// Gaussian rounding type can be specified:
var list = [123.535, 123.545];
equals(accounting.formatColumn(list, "$ ", null, null, null, null, 'gaussian').toString(), (["$ 123.54", "$ 123.54"]).toString(), "gaussian rounding works as expected");


// multi-dimensional array (formatColumn should be applied recursively):
var list = [[1, 100], [900, 9]];
equals(accounting.formatColumn(list).toString(), ([["$ 1.00", "$100.00"], ["$900.00", "$ 9.00"]]).toString(), "formatcolumn works on multi-dimensional array");
@@ -91,7 +114,6 @@ $(document).ready(function() {
ok((column[0].length === column[2].length && column[1].length === column[2].length), "formatColumn() with 3 random numbers returned strings of matching length, even with a weird custom `format` parameter");



});

});