Truncating Real Numbers


© Muhammad Ali Shah

  • Level: Advanced
  • Skills Required: Regular Expressions and Basic JavaScript


Abstract:

At various stages in web development, we need a way to round off decimal numbers. For example, the average score of a person taking various tests at some XYZ site can be calculated using JavaScript. But the resulting floating point number will be something like 45.358976902. Now, this is really ugly. Right? How can we round or even cut it off after two decimal places? That's exactly what we will see today.


Server Side Scripting:

One solution that immediately comes to ones mind is server side scripting. For example, if you are using Active Server Pages (ASP), you can use the FormatNumber method of VBScript, as shown below:

<%
score = "" & 45.358976902
Response.write FormatNumber (score, 2)
%>

As you can see, we can readily convert a floating point number to a string by concatenating it to a null string. The FormatNumber method accepts various optional parameters. The one we have used tells it to keep the number rounded upto two decimal places.


Client Side Scripting

"But this is supposed to be a JavaScript article!", you may object. Under most of the circumstances, I would recommend to use the above technique. However, for interactive calculations the whole idea of being interactive will be doomed if we rely on server side scripting for this.

For those, who have the courage to understand regular expressions, I present a client side based solution.

The simplest tool based solution is to use regular expressions. Just for the sake of review, a regular expression is defined in JavaScript using forward slashes. For example, /\d*.\d\d/ means any number of digits (\d represents a digit); followed by exactly one decimal point and two digits. Once you have defined a pattern this way, JavaScript tells the browser to create a regular expression object.

This object has a method objectName.exec(str). It accepts a string and tries to match the string with the pattern defined for the regular expression. The string str may have a substring that matches the pattern. If there is a match, then the result is an array. Otherwise, the result is null. The 0th index of the returned array is the first string that matched the pattern.


A Simple Example:

The following simple JavaScript code creates a string and a regular expression as explained above. Then it calls the exec() method to get a matched array. It then shows the resulting string.

Go To Page: 1 2


The copyright of the article Truncating Real Numbers in JavaScript is owned by . Permission to republish Truncating Real Numbers in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo