@@ -62,8 +62,16 @@ Decimal.prototype.toString = function() {
62
62
63
63
64
64
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 )
67
75
}
68
76
69
77
Decimal . _zeros = function ( str , exp ) {
@@ -105,12 +113,20 @@ var assert = {
105
113
assert . equals ( Decimal . _period ( '100135' , - 1 ) , '10013.5' )
106
114
assert . equals ( Decimal . _period ( '100135' , - 3 ) , '100.135' )
107
115
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' )
109
119
110
120
assert . equals ( Decimal . _zeros ( '1013' , 0 ) , '1013' )
111
121
assert . equals ( Decimal . _zeros ( '1013' , 1 ) , '10130' )
112
122
assert . equals ( Decimal . _zeros ( '1013' , 3 ) , '1013000' )
113
123
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' )
114
130
115
131
116
132
// Instanciation
0 commit comments