@@ -62,8 +62,16 @@ Decimal.prototype.toString = function() {
6262
6363
6464Decimal . _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
6977Decimal . _zeros = function ( str , exp ) {
@@ -105,12 +113,20 @@ var assert = {
105113assert . equals ( Decimal . _period ( '100135' , - 1 ) , '10013.5' )
106114assert . equals ( Decimal . _period ( '100135' , - 3 ) , '100.135' )
107115assert . 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
110120assert . equals ( Decimal . _zeros ( '1013' , 0 ) , '1013' )
111121assert . equals ( Decimal . _zeros ( '1013' , 1 ) , '10130' )
112122assert . 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