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