Use of asp Fix, Int, Round, and CInt Functions

Source: Internet
Author: User
Tags integer division

Fix (number) and Int (number) are both integer parts of the returned number.

When number is positive, the two return values are the same. For example, Fix (3.6) = 3, Int (3.6) = 3.
When number is a negative number, the Fix directly removes the fractional part. Int returns the first negative integer smaller than or equal to number. For example, Fix (-3.6) =-3, Int (-3.6) =-4.
Round (number, numdecimalplaces). The second parameter indicates that the number of digits on the right of the decimal point is rounded to five. This parameter can be omitted. The default value is 0, indicating that the integer is returned. The CInt (number) uses rounding to delete the fractional part.

If the second parameter of Round is omitted, 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 nearest even number. For example, Round (3.5) = 4, Round (4.5) = 4.
When number is a negative number, it can be understood as follows (assume 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 integer 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 codeThe Code is 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 Delete the fractional part of the number Parameter and return the result expressed as an integer. The difference between the 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 the number, and 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 the value rounded by the specified number of digits. Expression is required. The numeric expression is rounded down. Numdecimalplaces is optional. Number indicates the number of digits to the right of the decimal point. If this parameter is omitted, the Round function returns an integer.
Example:Copy codeThe Code is as follows: response. write round (3.14) '3
Response. write round (3.55) '4
Response. write round (3.1415, 3) '123'

ASP integer function

Number of Integers
As we all know, in the BASIC language, the system provides many standard functions, and "getting the entire function" is a very important function.
1. formats and functions of the "retrieve the entire function.
1. Format: INT (X)
2. function: obtain the maximum integer not greater than X.
3. Description: INT Is the function name and cannot be changed. X is the independent variable. It can be a numerical constant, a numerical variable, or a numerical expression.
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 decimals, INT
After the integer is obtained, the fractional part is rounded off without rounding. For a negative number with a decimal number, the integer is not directly rounded off, it is an integer 1 smaller than its integer. Of course, for a real integer, the value after INT remains unchanged.
II. Application of "getting the entire function"
1. Rounding numeric values
(1) The integer part of the X value is retained, and the fractional part is rounded to the nearest integer.
Expression: 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 can see that the key to rounding the integer function with INT is 0.5. From the perspective of the number axis, we can add 0.5 to a number, it is equivalent to moving the value 0.5 to the right. The first digit after the decimal point is less than 5 or greater than or equal to 5, determines whether the number is an integer during the moving to the right, because the value of the INT function is the maximum integer on the left. If an integer is passed, the result is the integer. Otherwise, the result is the same as that of the original number. In this way, the goal may be rounded down.
(2) retain two decimal places for the value of X, rounding 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 reserve is only different from the reservation above 1 in the decimal place. We only need to try to change the decimal place, so we use the method to first increase X by 100 times, then, use the first method to select decimal places, and then reduce the number by 100 times. This will not affect the basic size of the number, but also round the number.
Conclusion 1
N decimal places are retained for the X value. The general expression for rounding N + 1 decimal places is:
INT (x * 10 ^ N + 0.5)/X * 10 ^ N
2. Determine whether a single M can be divisible by N
For example, judge the parity of a number, that is, whether it can be divisible by 2
M = 25 M = 24
M/2 = 12.5 M/2 = 12
INT (M/2) = 12 INT (M/2)
Through the above expression, we can easily conclude that 25 is an odd number, 25/2 <> INT (25/2), 24 is an even number, 24/2 = INT (24/2 ), the INT function can remove the fractional part. For a number M, M/2 can be equal to INT (M/2) only when M can be divisible by 2 ), therefore, the expression of this question can be written:
When M/2 <> INT (M/2), M is an odd number
When M/2 = INT (M/2), M is an even number
Conclusion 2
Number M can be divisible by number N: M/N = INT (M/N)
M cannot count N integer division: M/N <> INT (M/N)
Iii. Differences between CINT (X) and FIX (X)
3. CINT (X) rounds the decimal part of X to an integer.
FIX (X) truncates the fractional part to get the integer
The following table compares 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:
Conclusion 3
When X> = 0, INT (X) has the same value,
When X is <0, the total value of INT (X) is 1;
CINT (X) is an integer rounded to the decimal part of X. Its function is the same as that of INT (X + 0.5 ).

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.