Skip to content

Commit 96a7c2e

Browse files
author
shinuza@localhost.localdomain
committed
Changed _zeros to _pos_exp
Changed _period to _neg_exp Added a __zero static method. An equivalent to '0' * 3 == '000' in Python
1 parent b6e2cbf commit 96a7c2e

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

decimaljs.js

+26-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Decimal.global = this;
1616

1717
Decimal.prototype._as_exp = function(exp) {
1818
var exp = exp + this.repr.exp;
19-
return parseInt(Decimal._zeros(this.repr.value, exp),10);
19+
return parseInt(Decimal._format(this.repr.value, exp),10);
2020
}
2121

2222
Decimal.prototype._get_repr = function() {
@@ -61,29 +61,33 @@ Decimal.prototype.toString = function() {
6161
}
6262

6363

64-
Decimal._period = function(str, position) {
64+
Decimal._neg_exp = function(str, position) {
6565
position = Math.abs(position);
6666
var offset = position - str.length;
6767
var sep = '.'
6868

6969
if(offset >= 0) {
70-
str = new Array(offset + 1 ).join('0') + str;
70+
str = Decimal.__zero(offset) + str;
7171
sep = 0 + '.';
7272
}
7373

74-
return str.substr(0, str.length - position) + sep + str.substr( -position )
74+
return str.substr(0, str.length - position) + sep + str.substr(-position)
7575
}
7676

77-
Decimal._zeros = function(str, exp) {
78-
var zeros = Array(exp + 1).join('0');
77+
Decimal._pos_exp = function(str, exp) {
78+
var zeros = Decimal.__zero(exp);
7979
return str + zeros;
8080
}
8181

8282
Decimal._format = function(str, exp) {
83-
var method = exp >= 0 ? '_zeros' : '_period';
83+
var method = exp >= 0 ? '_pos_exp' : '_neg_exp';
8484
return Decimal[method](str, exp);
8585
}
8686

87+
Decimal.__zero = function(exp) {
88+
return new Array(exp + 1).join('0');
89+
}
90+
8791

8892
var assert = {
8993
log : function() {
@@ -110,16 +114,21 @@ var assert = {
110114

111115

112116
// Static methods
113-
assert.equals(Decimal._period('100135',-1), '10013.5')
114-
assert.equals(Decimal._period('100135',-3), '100.135')
115-
assert.equals(Decimal._period('100135',-4), '10.0135')
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')
119-
120-
assert.equals(Decimal._zeros('1013',0), '1013')
121-
assert.equals(Decimal._zeros('1013',1), '10130')
122-
assert.equals(Decimal._zeros('1013',3), '1013000')
117+
assert.equals(Decimal._neg_exp('100135',-1), '10013.5')
118+
assert.equals(Decimal._neg_exp('100135',-3), '100.135')
119+
assert.equals(Decimal._neg_exp('100135',-4), '10.0135')
120+
assert.equals(Decimal._neg_exp('100135',-5), '1.00135')
121+
assert.equals(Decimal._neg_exp('100135',-6), '0.100135')
122+
assert.equals(Decimal._neg_exp('100135',-9), '0.000100135')
123+
124+
125+
assert.equals(Decimal._pos_exp('1013',0), '1013')
126+
assert.equals(Decimal._pos_exp('1013',1), '10130')
127+
assert.equals(Decimal._pos_exp('1013',3), '1013000')
128+
assert.equals(Decimal._pos_exp('1013',4), '10130000')
129+
assert.equals(Decimal._pos_exp('1013',5), '101300000')
130+
assert.equals(Decimal._pos_exp('1013',6), '1013000000')
131+
assert.equals(Decimal._pos_exp('1013',9), '1013000000000')
123132

124133
assert.equals(Decimal._format('1013',0), '1013')
125134
assert.equals(Decimal._format('1013',1), '10130')

0 commit comments

Comments
 (0)