Analysis of VBS two-digit addition Value Problem

Source: Internet
Author: User

A friend nicknamed the prophet Evening News liked to ask SOSO. The LV10 level is already relatively high. When I hung up QQ at night, I saw an update on his question. I clicked it and checked it. The problem was:
I wrote the following section about VBS: Copy codeThe Code is as follows: dim a, B, c
A = inputbox ("a", "please input ")
B = inputbox ("B", "please input ")
C = a + B
Msgbox (c)

However, the final result is 11. I know that the "+" in the last and second lines is not used as an operator. What should I do with this code?
You will be dismissive, cut, so simple program, who will not? So please do not look down, write this program independently to see if it is correct. Note that my title is the sum of two numbers, not the sum of two integers.

There are two satisfactory answers:Copy codeThe Code is as follows: dim a, B, c
A = inputbox ("a", "please input ")
B = inputbox ("B", "please input ")
C = CInt (a) + CInt (B)
Msgbox (c)

The other is given by people nicknamed "garbled:Copy codeThe Code is as follows: dim a, B, c
A = int (inputbox ("a", "please input "))
B = int (inputbox ("B", "please input "))
C = a + B
Msgbox c

Obviously, a CInt function and an Int function are used.

CInt FunctionCopy codeThe Code is as follows: When the fractional part of a value is exactly 0.5, the CInt function rounds to the closest even number. for example, 0.5 rounds to 0, 1.5 rounds to 2, and 3.5 rounds to 4.
CInt differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number.

CInt is different from the Fix and Int functions in deleting the decimal part of a value. Instead, it is rounded down. When the decimal part is equal to 0.5, The CInt always rounds it to an even number closest to the number. For example, 0.5 rounding is 0, 1.5 rounding is 2, and 3.5 rounding is 4.

Int, Fix FunctionsCopy codeThe Code is as follows: Both Int and Fix remove the fractional part of number and return the resulting integer value.

The difference between Int and Fix is that if number is negative, Int returns the first negative integer less than or equal to number, whereas Fix returns the first 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 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.

Which of the following is better?

In fact, both are not very clever. Even though the integer given by the input questioner (two integers) can both output the correct answer, if the input is a decimal number, both programs obviously do not output the correct answer.

The following is an answer that I think is "brilliant:Copy codeThe Code is as follows: Dim a, B, c
A = InputBox ("a", "please input ")
B = InputBox ("B", "please input ")
C = 0 + a + B
MsgBox c

Yes, but an extra 0 is added. I believe you already know the difference between "+" and "&" in VBS, but I will simply copy the document:

You can also use+Operator to connect two strings, but you must still use&To avoid confusion and provide easy-to-understand code.

This is because+It may be difficult to determine whether to perform addition or string connection.

The basic subtype of the expression determines+The operations performed by the operators are shown in the following table:

If Then
Both expressions are numerical values. Add
Both expressions are strings. Connection
One expression is a numeric value, and the other expression is a string. Add

The Return Value of the InputBox function is a string subtype. If the + sign is used directly, the result is the same as the & sign, that is, the connection string. However, if you add 0 to the front, the effect will be different. That is, the third case in the above table will change to the effect of the plus sign.

Of course, to ensure program rigor, we should also use the IsNumeric function to determine whether the input is a number. For simplicity, I will omit it here.
Original article: 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.