ASP Fix, Int, Round, CInt function use Instructions _asp Foundation

Source: Internet
Author: User
Tags numeric
Fix (number) and Int (numbers) are integral parts of the returned digits.

When number is positive, the two return the same value. For example: Fix (3.6) =3,int (3.6) = 3.
When number is negative, Fix removes the decimal part directly, 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 the beginning of the first few points on the right to rounding, can be omitted, the default is 0, that is, rounded return integer. CINT (number) removes the fractional part by rounding.

if the second argument of Round is omitted, then the Round and CINT functions are the same.

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 negative, you can understand this (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) function returns the integer portion of a digit. The number argument can be any valid numeric expression. Returns NULL if the number parameter contains null.
Cases:
Copy 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 the Fix functions delete the decimal part of the number parameter and return the result expressed as an integer. The difference between an int and the Fix function is that if the number argument is a negative number, the INT function returns the first negative integer less than or equal to 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 a value that is rounded by a specified number of digits. Expression is a required option. numeric expressions are rounded. Numdecimalplaces is optional. The number indicates how many digits to the right of the decimal point are rounded. If omitted, the Round function returns an integer.
Cases:
Copy 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 to take the whole function

Take the whole function
We all know that in basic language, the system provides us with a number of standard functions, and "Take the whole function" is one of the very important functions.
First, "Take the whole function" the format and the function.
1, Format: INT (X)
2, Function: Take the largest integer not greater than X
3, Description: Where int is the function name, not allowed to change, X is the independent variable, the form of a variety of, can be numeric constants, numerical variables, numeric 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 a positive number with a decimal part, INT
After this, the fractional part is dropped, but not rounded, for a negative number with a decimal, the int is not directly out of the decimal, but a whole number smaller than the entire part of the integer 1. Of course, for a true integer, the value of the int is unchanged.
Second, the application of "take the whole function"
1, rounding the value of the operation
(1) Keep the integer part of the X value and the fractional part rounded.
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 can see that using int to take the whole function to rounded up the function, The key is this 0.5, let's look at the number on the axis, add 0.5 to the value, and move 0.5 to the right. Whether the first digit is less than 5 or greater than or equal to 5 after the decimal point determines if the number passes through an integer as it moves to the right, because the INT function takes the largest integer to the left, and if an integer is passed, the result is the Integer, otherwise it will be the same as the original number direct int rounding. This may achieve the purpose of rounding.
(2) Keep two decimal digits for the value of X, rounded to the third decimal digit
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 reservation differs from the reservation above 1 in the decimal position, we just have to find a way to change the position of the decimal point, so we use the method is to expand the x 100 times times, and then the first method to choose the decimal, and then reduce the 100 times times, so that can not affect the basic size of the number, It can also be rounded.
Summary 1
To keep n decimal digits for x values, the general expression for the n+1 decimal rounding is:
INT (x*10^n+0.5)/x*10^n
2, a number of M can be divided by number n
For example: To determine the parity of a number, that is, whether 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 conclude from the above expression: 25 is odd, 25/2<>int (25/2), 24 is even, 24/2=int (24/2), INT function can give out the function of fractional part, for a few m, only if M can be divisible by 2, m/ 2 can be equal to int (M/2), so the subject expression can be written as:
When M/2 <>int (M/2), M is odd
When M/2=int (M/2), M is even
Summary 2
Number M can be divisible by number N: M/n=int (m/n)
Number M cannot count N divisible: m/n<>int (m/n)
Three, CINT (x), FIX (x) the difference
Three, CINT (x) round the X decimal part, and then rounded.
FIX (X) cut off the decimal part to take the whole
The following table is a comparison of three function values:

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,
When x<0 the value of int (X) is always smaller than 1;
CINT (x) is rounded rounding the decimal part of X with the same function as 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.