SoFunction
Updated on 2025-03-02

asp Fix, Int, Round, CInt function usage instructions

Fix(number) and Int(number) are both integer parts that return the number.

When number is a positive number, the return values ​​of both are the same. For example: Fix(3.6)=3, Int(3.6)=3.
When number is negative, Fix directly removes the fractional part, and Int returns the first negative integer less than or equal to number. For example: Fix(-3.6)=-3, Int(-3.6)=-4.
Round(number, numdecimalplaces), the second parameter indicates that rounding is implemented from the digit to the right of the decimal point, which can be omitted. The default is 0, that is, rounding returns an integer. CInt(number) Deletes the decimal part by rounding.

If the second parameter of Round is omitted, then the Round and CInt functions are the same.

When number is a positive number, Round(3.6)=4, CInt(3.6)=4. Note that when the decimal part happens to be 0.5, it is always rounded to the closest even number. For example, Round(3.5)=4, Round(4.5)=4.
When number is negative, it can be understood as follows (assuming n is positive):
Round(-n) = -Round(n), for example: Round(-3.5) = -4.
CInt(-n) = -CInt(n), for example: CInt(-99.8) = -100.

Several rounding functions in asp are: fix(), int(), round();
The Int(number) and Fix(number) functions return the integer part of the number. The number parameter can be any valid numeric expression. If the number parameter contains Null, Null is returned.
example:
Copy the codeThe code is as follows:

int(2.14) '2
fix(2.14) '2
int(2.54) '2
int(2.54) '2

Both the Int and Fix functions delete the decimal part of the number parameter and return the result expressed as an integer. The difference between Int and Fix functions is that if the number parameter is negative, the Int function returns the first negative integer less than or equal to number, while the Fix function returns the first negative integer greater than or equal to the number parameter. For example, Int converts -8.4 to -9, while the Fix function converts -8.4 to -8.
round(Expression[, numdecimalplaces]) returns the value rounded by the specified number of digits. Expression is a must. Numeric expressions are rounded. Numdecimalplaces are optional. The number indicates how many digits are rounded to the right of the decimal point. If omitted, the Round function returns an integer.
example:
Copy the codeThe code is as follows:

round(3.14) '3
round(3.55) '4
round(3.1415,3) ' 3.142

ASP rounding function

Take the integer function
As we all know, in the BASIC language, the system provides us with many standard functions, and "rounding function" is one of the very important functions.
1. The format and function of "round function".
1. Format: INT (X)
2. Function: Take the maximum integer not greater than X
3. Description: where INT is the function name and is not allowed to change. X is an independent variable. It has many forms, which can be numerical constants, numerical variables, and numerical expressions.
For example: INT (3.1416)=3
INT(3.8752)=3
INT(-3.14)=-4
INT(-3.85)=-4
From the above question, we can see that for positive numbers with decimal parts, INT
After rounding this, the decimal part is thrown away, but not rounding it. For negative numbers with decimals, INT does not directly throw away the decimal part after rounding, but takes an integer that is 1 smaller than its entire part. Of course, for real integers, the value after INT remains unchanged.
2. Application of "rounding function"
1. Round the numerical value
(1) Keep the integer part for the X value and the decimal part rounded.
The expression is: INT (X*100+0.5)
For example:
INT(3.1416+0.5)=INT(3.6416)=3
INT(3.8572+0.5)=INT(4.3572)=4
INT(-3.14+0.5)=INT(-2.64)=-3
INT(-3.85+0.5)=INT(-3.35)=-4
Through the analysis of the above examples, we may see that the key to using the INT rounding function to achieve rounding is this 0.5. From the perspective of the number axis, adding 0.5 to a number is equivalent to moving its value to the right by 0.5. According to whether the first digit after the decimal point is less than 5 or greater than or equal to 5, it determines whether this number has passed an integer during the movement to the right, because the value of the INT function is the largest integer on its left. If an integer passes through, the result is this integer, otherwise it is the same as the original number's direct INT rounding result. This will enable the purpose of rounding.
(2) Keep two decimal places for the value of X and round the third decimal places
Expression: INT (X*100+0.5)/100
For example:
INT(3.1416*100+0.5)/100
=INT(314.16+0.5)/100
=INT(314.66)/100
=314*100
=3.14
INT(3.8572*100+0.5)/100
=INT(385.72+0.5)/100
=INT(386.22)/100
=386/100
=3.86
This rounding retention is only different from the above 1 reservation at the decimal point position. We just need to find a way to change the decimal point position. Therefore, the method we use is to first expand X by 100 times, then choose the decimal according to the first method, and finally reduce it by 100 times. This will not only not affect the basic size of the number, but also round it.
Summary 1
The general expression of keeping N-digit decimal places for X values ​​and rounding N+1-th decimal places is:
INT(x*10^N+0.5)/X*10^N
2. Determine whether a number M can be divided by N
For example: determine the parity of a number, that is, whether it can be divided by 2
M=25 M=24
M/2=12.5 M/2=12
INT(M/2)=12 INT(M/2)
It is easy to draw a conclusion from the above expression: 25 is an odd number, 25/2<>INT(25/2), 24 is an even number, 24/2=INT(24/2), the INT function can achieve the function of discarding the decimal part. For a number M, M/2 can only be equal to INT(M/2), so the expression of this question can be written as:
When M/2 <>INT(M/2), M is an odd number
When M/2=INT(M/2), M is an even number
Summary 2
Number M can be divisible by number N: M/N=INT(M/N)
Number M cannot be divided by N: M/N<>INT(M/N)
3. The difference between CINT(X) and FIX(X)
3. CINT (X) rounds the X decimal part and rounds it.
FIX(X) cuts out the decimal part and rounds it
The following table is a comparison of the values ​​taken by three functions:

X INT(X) CINT(X) FIX(X)
3.26 3 3 3
3.76 3 4 3
-3.26 -4 -3 -3
-3.76 -4 -4 -3 :
Summary 3
When X>=0, the value of INT(X) is the same as it.
When X<0, the value of INT(X) is totally smaller than 1;
CINT(X) is rounded by rounding the decimal part of X, and its function is the same as INT(X+0.5).