Skip to content

Commit 725bc8e

Browse files
committed
postfix -> suffix, number rounding
1 parent 352dc0e commit 725bc8e

File tree

5 files changed

+54
-23
lines changed

5 files changed

+54
-23
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ wNumb - JavaScript Number & Money formatting
77

88
Documentation and examples are available on [refreshless.com/wnumb](http://refreshless.com/wnumb/).
99

10+
# Changelog
11+
12+
### 1.1.0 (*2017-02-04*)
13+
- Changed: Renamed `postfix` option to the proper `suffix`. `postfix` is remapped internally for backward compatibility;
14+
1015
# License
1116

1217
Licensed [WTFPL](http://www.wtfpl.net/about/), so free for personal and commercial use.

bower.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "wnumb",
33
"main": "wNumb.js",
4-
"version": "1.0.4",
5-
"homepage": "http://refreshless.com/wnumb/",
4+
"version": "1.1.0",
5+
"homepage": "https://refreshless.com/wnumb/",
66
"description": "wNumb - JavaScript Number & Money formatting",
77
"keywords": [
88
"javascript",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wnumb",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "wNumb - JavaScript Number & Money formatting",
55
"main": "wNumb.js",
66
"repository": {

test.html

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,25 @@
4545
decimals: 0
4646
});
4747

48-
equal ( Tester.to ( -50.999845 ), '-51' );
48+
equal ( Tester.to ( -50.999845 ), '-51' );
4949
equal ( Tester.to ( -0.0000001 ), '0' );
5050
});
5151

52+
test( "Testing rounding", function(){
53+
54+
var Tester = wNumb({
55+
decimals: 2
56+
});
57+
58+
equal ( Tester.to ( 8.905 ), '8.91' );
59+
equal ( Tester.to ( 8.9049999 ), '8.90' );
60+
equal ( Tester.to ( 8.9045 ), '8.90' );
61+
equal ( Tester.to ( 8.9044 ), '8.90' );
62+
equal ( Tester.to ( 8.9 ), '8.90' );
63+
equal ( Tester.to ( 1.275 ), '1.28' );
64+
equal ( Tester.to ( 1.27499 ), '1.27' );
65+
});
66+
5267
test( "Testing mark and thousand with longer strings", function(){
5368

5469
var Tester = wNumb({
@@ -65,11 +80,11 @@
6580

6681
});
6782

68-
test( "Testing prefix and postfix", function(){
83+
test( "Testing prefix and suffix", function(){
6984

7085
var Tester = wNumb({
7186
prefix: '$',
72-
postfix: ' p.p.',
87+
suffix: ' p.p.',
7388
decimals: 5
7489
});
7590

@@ -87,6 +102,13 @@
87102
equal ( Tester.from ( '1500 ' ), 1500 );
88103
equal ( Tester.from ( ' 1500 ' ), 1500 );
89104
equal ( Tester.from ( ' 1.50.0 ' ), false );
105+
106+
var Tester2 = wNumb({
107+
postfix: ' postfix!',
108+
decimals: 1
109+
});
110+
111+
equal ( Tester2.to ( 1 ), '1.0 postfix!' );
90112
});
91113

92114
test( "Testing prefix and with negativeBefore", function(){

wNumb.js

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
'use strict';
2222

23-
var
24-
/** @const */ FormatOptions = [
23+
var FormatOptions = [
2524
'decimals',
2625
'thousand',
2726
'mark',
2827
'prefix',
29-
'postfix',
28+
'suffix',
3029
'encoder',
3130
'decoder',
3231
'negativeBefore',
@@ -47,7 +46,7 @@ var
4746
return input.substring(0, match.length) === match;
4847
}
4948

50-
// Check is a string ends in a specified postfix.
49+
// Check is a string ends in a specified suffix.
5150
function strEndsWith ( input, match ) {
5251
return input.slice(-1 * match.length) === match;
5352
}
@@ -65,16 +64,19 @@ var
6564
}
6665

6766
// Provide rounding-accurate toFixed method.
68-
function toFixed ( value, decimals ) {
69-
var scale = Math.pow(10, decimals);
70-
return ( Math.round(value * scale) / scale).toFixed( decimals );
67+
// Borrowed: http://stackoverflow.com/a/21323330/775265
68+
function toFixed ( value, exp ) {
69+
value = value.toString().split('e');
70+
value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)));
71+
value = value.toString().split('e');
72+
return (+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp))).toFixed(exp);
7173
}
7274

7375

7476
// Formatting
7577

7678
// Accept a number as input, output formatted string.
77-
function formatTo ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) {
79+
function formatTo ( decimals, thousand, mark, prefix, suffix, encoder, decoder, negativeBefore, negative, edit, undo, input ) {
7880

7981
var originalInput = input, inputIsNegative, inputPieces, inputBase, inputDecimals = '', output = '';
8082

@@ -151,9 +153,9 @@ var
151153
output += inputBase;
152154
output += inputDecimals;
153155

154-
// Apply the postfix.
155-
if ( postfix ) {
156-
output += postfix;
156+
// Apply the suffix.
157+
if ( suffix ) {
158+
output += suffix;
157159
}
158160

159161
// Run the output through a user-specified post-formatter.
@@ -166,7 +168,7 @@ var
166168
}
167169

168170
// Accept a sting as input, output decoded number.
169-
function formatFrom ( decimals, thousand, mark, prefix, postfix, encoder, decoder, negativeBefore, negative, edit, undo, input ) {
171+
function formatFrom ( decimals, thousand, mark, prefix, suffix, encoder, decoder, negativeBefore, negative, edit, undo, input ) {
170172

171173
var originalInput = input, inputIsNegative, output = '';
172174

@@ -198,10 +200,10 @@ var
198200
inputIsNegative = true;
199201
}
200202

201-
// Remove the postfix.
203+
// Remove the suffix.
202204
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice
203-
if ( postfix && strEndsWith(input, postfix) ) {
204-
input = input.slice(0, -1 * postfix.length);
205+
if ( suffix && strEndsWith(input, suffix) ) {
206+
input = input.slice(0, -1 * suffix.length);
205207
}
206208

207209
// Remove the thousand grouping.
@@ -255,6 +257,10 @@ var
255257
var i, optionName, optionValue,
256258
filteredOptions = {};
257259

260+
if ( inputOptions['suffix'] === undefined ) {
261+
inputOptions['suffix'] = inputOptions['postfix'];
262+
}
263+
258264
for ( i = 0; i < FormatOptions.length; i+=1 ) {
259265

260266
optionName = FormatOptions[i];
@@ -323,7 +329,6 @@ var
323329
return method.apply('', args);
324330
}
325331

326-
/** @constructor */
327332
function wNumb ( options ) {
328333

329334
if ( !(this instanceof wNumb) ) {
@@ -347,7 +352,6 @@ var
347352
};
348353
}
349354

350-
/** @export */
351355
return wNumb;
352356

353357
}));

0 commit comments

Comments
 (0)