Float data is forcibly converted to an int integer or floatint type.
Problem: the following code is used to obtain the commodity size between two Decimal types. The result is occasionally incorrect (please note that it is occasionally)
Decima t1;
Decima t2;
Int shang = Convert. ToInt32 (t1/t2 );
Solution: When Decimal type data is forcibly converted to an INT type Integer, the process is rounded down. As follows, the Math. Truncate method must be used to obtain the integer. Therefore, you must use this method to get the final value for the region.
Problem code:
Decima t1 = 1.2 m;
Convert. ToInt32 (t1) returns 1
Decima t2= 1.7 m;
Convert. ToInt32 (t2) returns 2
After the code is changed to the following:
Decima t1 = 1.2 m;
Convert. ToInt32 (Math. Truncate (t1) returns 1
Decima t2= 1.7 m;
Convert. ToInt32 (Math. Truncate (t2) returns 1