@@ -63,39 +63,54 @@ Decimal.prototype.sub = function(target) {
63
63
64
64
Decimal . prototype . mul = function ( target ) {
65
65
target = Decimal ( target ) ;
66
+
67
+ var calc = String ( this . repr . value * target . repr . value )
68
+
69
+ return Decimal . _format ( calc , this . repr . exp + target . repr . exp ) ;
70
+ }
71
+
72
+ Decimal . prototype . div = function ( target ) {
73
+ target = Decimal ( target ) ;
66
74
67
75
var ops = [ this , target ] ;
68
- var fst = ops [ 0 ] . repr . value ;
69
- var snd = ops [ 1 ] . repr . value ;
70
- var calc = String ( fst * snd )
76
+ ops . sort ( function ( x , y ) { return x . repr . exp - y . repr . exp } ) ;
77
+
78
+ var smallest = ops [ 0 ] . repr . exp ;
79
+ var biggest = ops [ 1 ] . repr . exp ;
71
80
72
- return Decimal . _format ( calc , ops [ 0 ] . repr . exp + ops [ 1 ] . repr . exp ) ;
81
+ var fst = Decimal . _format ( ops [ 1 ] . repr . value , biggest - smallest ) * 1 ;
82
+ var snd = ops [ 0 ] . repr . value * 1 ;
83
+
84
+ var calc = String ( fst / snd ) ;
85
+
86
+ return Decimal . _format ( calc , smallest ) ;
73
87
}
74
88
75
-
76
89
Decimal . prototype . toString = function ( ) {
77
90
return this . internal ;
78
91
}
79
92
80
-
81
93
Decimal . _neg_exp = function ( str , position ) {
82
94
position = Math . abs ( position ) ;
95
+
83
96
var offset = position - str . length ;
84
- var sep = '.'
97
+ var sep = Decimal . SEPARATOR ;
85
98
86
99
if ( offset >= 0 ) {
87
100
str = Decimal . __zero ( offset ) + str ;
88
101
sep = '0.' ;
89
102
}
90
103
91
104
var length = str . length ;
105
+ var head = str . substr ( 0 , length - position ) ;
106
+ var tail = str . substring ( length - position , length ) . replace ( / \. / g, '' ) ;
92
107
93
- return str . substr ( 0 , length - position ) + sep + str . substring ( length - position , length ) ;
108
+ return head + sep + tail ;
94
109
}
95
110
96
111
Decimal . _pos_exp = function ( str , exp ) {
97
112
var zeros = Decimal . __zero ( exp ) ;
98
- return str + zeros ;
113
+ return String ( str + zeros ) ;
99
114
}
100
115
101
116
Decimal . _format = function ( num , exp ) {
@@ -107,9 +122,11 @@ Decimal.__zero = function(exp) {
107
122
return new Array ( exp + 1 ) . join ( '0' ) ;
108
123
} ;
109
124
125
+ Decimal . SEPARATOR = '.' ;
126
+
110
127
( function ( ) {
111
128
//Generics
112
- var methods = [ 'add' , 'mul' , 'sub' ] ;
129
+ var methods = [ 'add' , 'mul' , 'sub' , 'div' ] ;
113
130
114
131
for ( var i = 0 ; i < methods . length ; i ++ ) {
115
132
( function ( method ) {
0 commit comments