C # decimal point of Division

Source: Internet
Author: User

In C #, Division does not retain the decimal point by default,

Decimal result = 100/1000; // result = 0;

The decimal point must be retained.

Decimal result = 100 m/1000;

M represents decimal.

What should I do if it is a variable? This requires math. Round ()

Int x = 120;

Int y= 100000;

Decimal result = (decimal) x/y; // (decimal) x/y indicates converting X to decimal and then performing Division operations. Int division will lose the decimal point.

However, there are too many digits after the decimal point. You need to handle it. At this time, you need math. Round ()

Decimal result = math. Round (decimal) x/y, 2); 2 indicates that the second decimal point is retained

Bytes --------------------------------------------------------------------------------------------------------

In the C # and method, the type of the value obtained after the "/" division is related to the type of the divisor and the divisor. For example:

Int A = 4;

Int B = 5;

Float c = A/B;

The result is 0 (because the int division operation is performed first, The result 0 is obtained, and the result is converted to float 0 ;);

In short, the obtained number is an integer, and the type of the value obtained after division is found to be related to the divisor type and the divisor type. Therefore, it should be written:

Float a = 3;

Float B = 5;

Float c = A/B;

In this way, we can draw a correct conclusion!

In int, 2/5 of the result is 0.

Because the left side of the expression (float num = 2/5) is float, and INT can be implicitly converted to float, C # converts 0 to float: 0.0.

If you want to get the correct result, you must set at least one number on the right of the equal sign to float: Add a decimal point, or add F:

2.0/5

Or

2.0/5.0

Or

2f/5

 

In C #, Division does not retain the decimal point by default,

Decimal result = 100/1000; // result = 0;

The decimal point must be retained.

Decimal result = 100 m/1000;

M represents decimal.

What should I do if it is a variable? This requires math. Round ()

Int x = 120;

Int y= 100000;

Decimal result = (decimal) x/y; // (decimal) x/y indicates converting X to decimal and then performing Division operations. Int division will lose the decimal point.

However, there are too many digits after the decimal point. You need to handle it. At this time, you need math. Round ()

Decimal result = math. Round (decimal) x/y, 2); 2 indicates that the second decimal point is retained

Bytes --------------------------------------------------------------------------------------------------------

In the C # and method, the type of the value obtained after the "/" division is related to the type of the divisor and the divisor. For example:

Int A = 4;

Int B = 5;

Float c = A/B;

The result is 0 (because the int division operation is performed first, The result 0 is obtained, and the result is converted to float 0 ;);

In short, the obtained number is an integer, and the type of the value obtained after division is found to be related to the divisor type and the divisor type. Therefore, it should be written:

Float a = 3;

Float B = 5;

Float c = A/B;

In this way, we can draw a correct conclusion!

In int, 2/5 of the result is 0.

Because the left side of the expression (float num = 2/5) is float, and INT can be implicitly converted to float, C # converts 0 to float: 0.0.

If you want to get the correct result, you must set at least one number on the right of the equal sign to float: Add a decimal point, or add F:

2.0/5

Or

2.0/5.0

Or

2f/5

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.