Convert decimal to integer

string retval;

retval = Decimal.ToInt32(1899.99M).ToString();

the output is 1899. But I want the decimal size to be greater than .5, then the output is 1900, and then 1899. How can I do this? thanks in advance!

+3
source share
4 answers

When using Math.Roundyou have two rounding options x.5:

Banker round / round to even. It avoids prejudice, sometimes rounding up (1.5 => 2) and sometimes rounding down (0.5 => 0), it is used by default if you do not specify a parameter.

Int32 i=Math.Round(d, MidpointRounding.ToEven);

The rounding you study at school, where it is always rounded to infinity (0.5 => 1, -0.5 => - 1)

Int32 i=Math.Round(d, MidpointRounding.AwayFromZero);
+3

- :

Double d = 1899.99;
Int32 i = Math.Round(d);
String retval = i.ToString(CultureInfo.InvariantCulture);
+1
Decimal.ToInt32(Math.Round(1899.99M)).ToString();
0

Source: https://habr.com/ru/post/1786160/


All Articles