Skip to content

Commit b6e2cbf

Browse files
author
shinuza@localhost.localdomain
committed
Fixed Decimal._format
Fixed Decimal._period
1 parent 69b4f1e commit b6e2cbf

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

decimaljs.js

+19-3
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ Decimal.prototype.toString = function() {
6262

6363

6464
Decimal._period = function(str, position) {
65-
position = -1 * position;
66-
return str.substr(0, str.length - position) + '.' + str.substr( -position )
65+
position = Math.abs(position);
66+
var offset = position - str.length;
67+
var sep = '.'
68+
69+
if(offset >= 0) {
70+
str = new Array(offset + 1 ).join('0') + str;
71+
sep = 0 + '.';
72+
}
73+
74+
return str.substr(0, str.length - position) + sep + str.substr( -position )
6775
}
6876

6977
Decimal._zeros = function(str, exp) {
@@ -105,12 +113,20 @@ var assert = {
105113
assert.equals(Decimal._period('100135',-1), '10013.5')
106114
assert.equals(Decimal._period('100135',-3), '100.135')
107115
assert.equals(Decimal._period('100135',-4), '10.0135')
108-
116+
assert.equals(Decimal._period('100135',-5), '1.00135')
117+
assert.equals(Decimal._period('100135',-6), '0.100135')
118+
assert.equals(Decimal._period('100135',-9), '0.000100135')
109119

110120
assert.equals(Decimal._zeros('1013',0), '1013')
111121
assert.equals(Decimal._zeros('1013',1), '10130')
112122
assert.equals(Decimal._zeros('1013',3), '1013000')
113123

124+
assert.equals(Decimal._format('1013',0), '1013')
125+
assert.equals(Decimal._format('1013',1), '10130')
126+
assert.equals(Decimal._format('1013',3), '1013000')
127+
assert.equals(Decimal._format('1013',-1), '101.3')
128+
assert.equals(Decimal._format('1013',-3), '1.013')
129+
assert.equals(Decimal._format('1013',-5), '0.01013')
114130

115131

116132
// Instanciation

0 commit comments

Comments
 (0)