ASP numeric functions and how to use them

ASP Best Practices

Unraveling the confusion between cInt, cSng, cDbl, cCur, Int, Fix, Round. What results to expect from each of these functions.

Date : 2006-07-05
There is a quite a bit of confusion among VBScript programmers over numeric functions. VBScript has a number of functions for converting to and from and working with numbers. The most often misused functions for these conversions are cInt(), int(), fix(), round(), cSng, cLng(), cDbl(), and cCur(). Let’s look at each of these and determine when it is best to use.

Before getting into the function specifics we need to make sure that we all are on the same page about the terms we will be using. The types we will be dealing with are described in the following table.

    Integer - Contains integer in the range -32,768 to 32,767.
    Long - Contains integer in the range -2,147,483,648 to 2,147,483,647.
    Single - Contains a single-precision, floating-point number in the range -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values.
    Double - Contains a double-precision, floating-point number in the range -1.79769313486232E308 to -4.94065645841247E-324 for negative values; 4.94065645841247E-324 to 1.79769313486232E308 for positive values.
    Currency - -922,337,203,685,477.5808 to 922,337,203,685,477.5807.

cInt(expression) this function takes any input and does it’s best to convert it to an integer. If it is unable to convert the expression to an integer you will get a “Type Mismatch” error. For instance cInt(“red”) will return an error while cInt(“123.230”) will return 123 as an integer not a string.

cSng(expression) is similar to cInt in that it will accept most anything and try to convert it to a number of type Single. This function will also return an error for something that is non numeric.

cDbl(expression) is on the same bandwagon. It will accept most anything and convert it to a number of type Double. Again an error is raised when a non numeric expression is passed.

cCur(expression) is the matching function when dealing with data of type Currency.

For the most part all of these functions are expecting to find a string with numbers in it, they are going to convert them to the specified type. A notable exception to this is you can pass a dollar sign and all of these functions will still act normally. For instance cCur(“r55.50”) will return an error while cCur(“$55.50” will return 55.50. This is not just true of the cCur function either. cInt(“$55.50”) will return 55. I have not had a chance to test this with other currency characters but imagine that if you have the correct country settings it will handle other currency characters. Unfortunately it will not handle a percent sign.
Now, that takes care of all of the numeric conversion functions. (that’s what the “c” stands for: “convert”).

That leave us with Int() and Fix(). Again these 2 functions are often confused. Most programmers pick one and use it all the time but there are really 2 different purposes for these functions.

The similarity is that they both remove the decimal part of the number. For positive numbers they act identically both simply dropping the decimal. For negative numbers, however, Int() will return the first negative integer that is less than or equal to the original number. Fix() on the other hand will return the first negative integer that is greater than or equal to the original number. Round() does what we would logically think, it rounds the number to the nearest integer whether positive or negative.

Here are some examples:

Int(99.8) = 99
Fix(99.8) = 99
Int(99.2) = 99
Fix(99.2) = 99
Int(-99.2) = -100
Fix(-99.2) = -99
Int(-99.8) = -100
Fix(-99.8) = -99
And just for kicks:
Int(-99.5) = -100
Fix(-99.5) = -99

So you see while identical when dealing with positive numbers Fix handles negative numbers slightly differently. If you find it hard to remember just think of Fix() as trying to get the number as close to zero as possible while Int() wants the number to be as low as possible.

When it comes down to it many times people don’t want either of these functions when converting from a decimal number to an integer usually the most logical thing is to round the number. That is where the Round() function comes in.

Round(99.5) = 99
Round(99.4) = 99
Round(99.6) = 100
Round(-99.5) = -99
Round(-99.4) = -99
Round(-99.6) = -100

So by knowing what each of these functions does it will make it easier for you to use them correctly and not be surprised by the results.

Comments :

No comments yet
  • Search For Articles