@@ -16,7 +16,7 @@ Decimal.global = this;
16
16
17
17
Decimal . prototype . _as_exp = function ( exp ) {
18
18
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 ) ;
20
20
}
21
21
22
22
Decimal . prototype . _get_repr = function ( ) {
@@ -61,29 +61,33 @@ Decimal.prototype.toString = function() {
61
61
}
62
62
63
63
64
- Decimal . _period = function ( str , position ) {
64
+ Decimal . _neg_exp = function ( str , position ) {
65
65
position = Math . abs ( position ) ;
66
66
var offset = position - str . length ;
67
67
var sep = '.'
68
68
69
69
if ( offset >= 0 ) {
70
- str = new Array ( offset + 1 ) . join ( '0' ) + str ;
70
+ str = Decimal . __zero ( offset ) + str ;
71
71
sep = 0 + '.' ;
72
72
}
73
73
74
- return str . substr ( 0 , str . length - position ) + sep + str . substr ( - position )
74
+ return str . substr ( 0 , str . length - position ) + sep + str . substr ( - position )
75
75
}
76
76
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 ) ;
79
79
return str + zeros ;
80
80
}
81
81
82
82
Decimal . _format = function ( str , exp ) {
83
- var method = exp >= 0 ? '_zeros ' : '_period ' ;
83
+ var method = exp >= 0 ? '_pos_exp ' : '_neg_exp ' ;
84
84
return Decimal [ method ] ( str , exp ) ;
85
85
}
86
86
87
+ Decimal . __zero = function ( exp ) {
88
+ return new Array ( exp + 1 ) . join ( '0' ) ;
89
+ }
90
+
87
91
88
92
var assert = {
89
93
log : function ( ) {
@@ -110,16 +114,21 @@ var assert = {
110
114
111
115
112
116
// 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' )
123
132
124
133
assert . equals ( Decimal . _format ( '1013' , 0 ) , '1013' )
125
134
assert . equals ( Decimal . _format ( '1013' , 1 ) , '10130' )
0 commit comments