<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Javascript .toFixed() Example</title> </head> <body> <script type="text/javascript"> // Create a string representation of the number // that we want to format. var StringValue = "1234.56789"; // Create a numeric representation of the number // that we want to format by parsing the float // value out of the string value. var NumericValue = parseFloat( StringValue ); // The .toFixed() is a method of the Numeric object. // But, let's try to call it on the string to see if // there is any implicit type-casting done for us. try { document.write( StringValue.toFixed( 2 ) ); } catch( Error ){ document.write( "String value did not work<br />" ); } // We know that it works on the numeric object, // but let's loop over it to see how it works before // and after we pass the known number of digits. for (intI = 0 ; intI < 10 ; intI++){ document.write( NumericValue.toFixed( intI ) + "<br />" ); } </script> </body> </html>