[DOTNET skill series] 3. C # Arithmetic Operators, value assignment operators, and type conversion

Source: Internet
Author: User
Tags dotnet

Rule: Arithmetic Operators and value assignment operators must have the same operands on both sides of the operation, and the operation result is of the same type as the operands..

// Comparison of arithmetic operations of the int and double types
Int num1 = 10; int num2 = 3; int mod = num1 % num2; // 1 // The Int type is obtained after division, then int type is automatically converted to double type double result = num1/num2; // 3 console. writeline ("{0 }%{ 1 }={ 2}", num1, num2, MoD); console. writeline ("{0}/{1} = {2}", num1, num2, result); double num3 = 10; double num4 = 3; // here, the division operation is performed first to obtain the double type, so the result is different from that of the int type division operation. Double result2 = num3/num4; // 3. 333... console. writeline ("{0}/{1} = {2}", num1, num2, result2 );

When the following conditions are met, the system automatically converts the data types.

  • Compatible with both types, such as int and double
  • The target type is greater than the source type.

That is to say, when the types on both sides are inconsistent, it is not impossible to calculate, but to see whether they comply with the automatic conversion rules.

 

Automatic conversion: automatic conversion starts from low to high, and automatic conversion does not lose precision. If the precision may be lost during conversion, the system will not automatically convert, but will give a compilation error to the programmer to force conversion.

1. Int can be converted to float and double. The Int range is the smallest (about +-10), and the double (about +-308)

2. decimal and double cannot be converted because of different precision.

Note: The conversion here is only determined based on the type, that is, the double type cannot be automatically converted to the int type, regardless of whether the value of the variable exceeds the int range..

 

TIPS:

1. If you want to convert a formula from int to double, as long as one of them is of the double type, all types of operations are of the double type.

Double result = num1 * 1.0/num2; // The result here is also 3.33 ....
Double result2 = (double) num1/num2; // forced conversion

2. Force conversion of lost progress is not necessarily meaningless. For example, sometimes only integers are charged instead of scores.

// Application Scenario where precision is lost int money = (INT) 99.12; // 99 console. writeline (money );

3. Data type conversion class: Convert. toint32

int ipi = Convert.ToInt32(3.14);            Console.WriteLine(ipi);
int ipi2 = Convert.ToInt32("10");            Console.WriteLine(ipi2);
Int ipi2 = convert. toint32 ("3.14"); // runtime error, formatexception console. writeline (ipi2 );

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.