c# Numericupdown to pass a value to an integer

瘦欲@ 提交于 2019-12-05 11:48:08

NumericUpDown.Value returns decimal so you need to round and convert to integer

Use this,

int count = Convert.ToInt32(Math.Round(numericUpDown1.Value, 0));
updateCount(count);

You can convert directly to the integer type if you have set Increment to integer (no decimal points) number, otherwise it is safe to round first before conversion.

So without round

int count = Convert.ToInt32(numericUpDown1.Value);
updateCount(count);

The issue is that the numeric value of the NumericUpDown control is decimal and you wanted to assign it to a integer type. You should do a TypeCast to assign it. For the same there is a Convert class you can use it like this

int count = Convert.ToInt32(numericUpDown1.Value);

int count = (int)numericUpDown1.Value;

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!