<Script language ="JavaScript">
Alert (87593.21-3.12 );
</SCRIPT>
Correctly displayed: 87590.09
But the result is: 87590.09000000001.
Tofixed can be used for processing, such:
<Script language ="JavaScript">
Alert (87593.21-3.12). tofixed (2 ));
</SCRIPT>
---------------------------------
Tofixed method problems:
<Script language ="JavaScript">
Alert (0.009.tofixed (2 ));
</SCRIPT>
The calculation result is 0.00, But 0.01 is required.
Solution:
Number. Prototype. tofixed =Function(D)
{
VaRS =This+"";If(! D) d = 0;
If(S. indexof (".") =-1) S + ="."; S + =NewArray (D + 1). Join ("0");
If(NewRegexp ("^ (-| \\+ )? (\ D + (\. \ D {0 ,"+ (D + 1) +"})?) \ D * $"). Test (s ))
{
VaRS ="0"+ Regexp. $2, PM = Regexp. $1, A = Regexp. $3. length, B =True;
If(A = d + 2) {A = S. Match (/\ D/G );If(Parseint (A [A. Length-1])> 4)
{
For(VaRI = A. Length-2; I> = 0; I --) {A [I] = parseint (A [I]) + 1;
If(A [I] = 10) {A [I] = 0; B = I! = 1 ;}Else Break;}
}
S = A. Join (""). Replace (NewRegexp ("(\ D +) (\ D {"+ D +"}) \ D $"),"$1. $2");
}If(B) S = S. substr (1 );Return(PM + S). Replace (/\. $ /,"");}Return This+"";
};