Fix(number) and Int(number) both return the integer part of the number, Round(number, numdecimalplaces). The second parameter indicates the number to the right of the decimal point to perform rounding. It can be omitted. The default is 0, that is, rounding returns an integer. Fix(number) and Int(number) both return the integer part of a number.
When number is a positive number, the two return values are the same. For example: Fix(3.6)=3, Int(3.6)=3.
When number is a negative number, Fix directly removes the decimal 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 the number to the right of the decimal point for rounding, which can be omitted. The default is 0, which means rounding returns an integer. CInt(number) uses rounding to remove the decimal part.
If the second parameter of Round is omitted, the functions of Round and CInt are the same.
When number is a positive number, Round(3.6)=4, CInt(3.6)=4. Note that when the decimal part is exactly 0.5, it is always rounded to the nearest even number. For example, Round(3.5)=4, Round(4.5)=4.
When number is a negative number, it can be understood like this (assuming n is a positive number):
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 a number. The number parameter can be any valid numeric expression. If the number parameter contains Null, Null is returned.
example:
Copy the code code as follows:
response.write int(2.14) '2
response.write fix(2.14) '2
response.write int(2.54) '2
response.write int(2.54) '2
Both the Int and Fix functions remove the decimal part of the number argument and return the result as an integer. The difference between the Int and Fix functions is that if the number parameter is a negative number, 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, and the Fix function converts -8.4 to -8.
round(Expression[, numdecimalplaces]) returns a number rounded to the specified number of digits. Expression is required. Numeric expressions are rounded. Numdecimalplaces are optional. The number indicates how many digits to the right of the decimal point are used for rounding. If omitted, the Round function returns an integer.
example:
Copy the code code as follows:
response.write round(3.14) '3
response.write round(3.55) '4
response.write round(3.1415,3) ' 3.142
ASP rounding function
rounding function
Everyone knows that in the BASIC language, the system provides us with many standard functions, and the rounding function is one of the very important functions.
1. The format and function of the rounding function.
1. Format: INT (X)
2. Function: Get the largest integer not greater than X
3. Description: INT is the function name, which cannot be changed. X is the independent variable, which has various forms and 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, the decimal part is rounded off, but no rounding is performed. For negative numbers with decimals, INT does not directly round off the decimal after rounding, but takes an integer that is 1 smaller than its integral part. Of course, for real integers, their value does not change after INT.
2. Application of rounding function
1. Round off values
(1) Keep the integer part of the X value and round off the decimal part.
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
By analyzing the above example, we may see that the key to using the INT rounding function to achieve the rounding function is the 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 The first digit after the decimal point is a small 5 or greater than or equal to 5, determines whether the number passes through an integer during the movement to the right, because the INT function takes the value of the largest integer to its left. If it passes through an integer, the result will be this integer, otherwise It is the same as the result of direct INT rounding of the original number. In this way, it is possible to achieve the purpose of rounding.
(2) Keep two decimal places for the value of X, and round to the third decimal place.
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
The only difference between this rounding retention and the retention of 1 above is the decimal point position. We only need to find a way to change the decimal point position, so the method we use is to first expand X by 100 times, and then make the rounding according to the first method. Decimal, and finally reduce it by 100 times, so that the basic size of the number is not affected and it can be rounded.
Summary 1
The general expression of retaining N decimal places for the X value and rounding to the N+1th decimal place is:
INT(x*10^N+0.5)/X*10^N
2. Determine whether a number M is divisible by a number N
For example: Determine the parity of a number, that is, whether it is divisible 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 the 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 round off the decimal part Function, for a number M, M/2 can be equal to INT(M/2) only when M is divisible by 2, so the expression in 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
The number M is divisible by the number N: M/N=INT(M/N)
The number M is not divisible by N: M/N<>INT(M/N)
3. The difference between CINT(X) and FIX(X)
3. CINT(X) rounds the decimal part of X and then rounds it to an integer.
FIX(X) truncate the decimal part and round
The following table is a comparison of the values of the 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 always less than 1;
CINT(X) rounds the decimal part of X, and its function is the same as INT(X+0.5)