Analysis of _vbs of VBS two number addition

Source: Internet
Author: User
A nickname for the prophet of the evening news friends like to play Soso ask, Grade LV10, has been relatively high. Hanging QQ at night, see his ask to have an update, on the point of a look, the problem is:
I wrote the following section of VBS
Copy Code code as follows:

Dim a,b,c
A=inputbox ("A", "Please input")
B=inputbox ("B", "Please input")
C=a+b
MsgBox (c)

But the final result is 11, I know it must be the penultimate line of "+" is not as an operator, I would like to ask this code how to write the correct?
You'll certainly be dismissive, cut, so simple program, who wouldn't? Then please don't look down and write this program independently to see if it is right. Notice that my title is added with two numbers, not two integers.

There are two satisfactory answers, one from my friend:
Copy Code code as follows:

Dim a,b,c
A=inputbox ("A", "Please input")
B=inputbox ("B", "Please input")
C=cint (a) +cint (b)
MsgBox (c)

Another is the nickname "garbled" given by the person:
Copy Code code as follows:

Dim a,b,c
A=int (InputBox ("A", "Please input"))
B=int (InputBox ("B", "Please input"))
C=a+b
MsgBox C

Obviously, one uses the CInt function, one with the INT function.

CInt Function
Copy Code code as follows:

When the fractional part of a value is exactly 0.5, the CINT function rounds to the closest number. For example, the 0.5 rounds to 0, the 1.5 rounds to 2, and the 3.5 to 4.
CInt differs from the Fix and INT functions, which truncate, rather than round, the fractional part a number.

CInt differs from the fix and int functions in removing the decimal part of a value, but by rounding it. When the decimal part is exactly equal to 0.5, CInt always rounded it to the nearest even number. For example, 0.5 is rounded to 0, and 1.5 is rounded to 2, and 3.5 is rounded to 4.

INT, Fix functions
Copy Code code as follows:

Both Int and Fix remove the fractional part of the resulting integer value.

The difference between Int and Fix is, the-if number is negative, INT returns the The "a"-negative integer less or than l to number, whereas Fix returns the "the" negative integer greater than or equal to number. For example, Int converts-8.4 to-9, and Fix converts-8.4 to-8.

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.

One is rounded, one is to delete the decimal part, which is more clever?

In fact, two are not very clever. Although the input of the questioner gives an integer (two 1), both outputs the correct answer, but if the input is a decimal, neither program will clearly output the correct answer.

Here's an answer I think is more "brilliant":
Copy Code code as follows:

Dim A,b,c
A = InputBox ("A", "Please input")
b = InputBox ("b", "Please input")
c = 0 + A + b
MsgBox C

Yes, just an extra 0. The difference between + and & in VBS I'm sure you already know, but I'll simply copy the document:

Although you can also use the + operator to connect two strings, you should still use the & operator to concatenate strings to avoid confusion and provide understandable code.

This is because when the + operator is used, it may not be possible to determine whether an addition or a string connection is made.

The basic subtype of an expression determines the operation of the + operator, as shown in the following table:

If The
Two of expressions are numeric Add
All two expressions are strings Connection
An expression is a numeric value and another expression is a string Add

The return value of the InputBox function is a string subtype, and the effect is the same as the & number if directly using the + number, that is, the connection string. But if you add a 0 to the front, the effect is different, that is, the third in the table above, become the effect of the plus.

Of course, in order to program rigorous, you should also use the IsNumeric function to determine whether the input is a number, for simplicity, I have omitted here.
Original: http://demon.tw/programming/vbs-add-two-number.html

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.