How would you round up a decimal or float to an integer. For instance...
0.0 => 0
0.1 => 1
1.1 => 2
1.7 => 2
2.1 => 3
Etc.
How would you round up a decimal or float to an integer. For instance...
0.0 => 0
0.1 => 1
1.1 => 2
1.7 => 2
2.1 => 3
Etc.
Simple, use Math.Ceiling
:
var wholeNumber = (int)Math.Ceiling(fractionalNumber);
Before saying it does not work, you have to check that ALL VALUES in the operation are double type. Here is an example in C#:
int speed= Convert.ToInt32(Math.Ceiling((double)distance/ (double)time));
Math.Ceiling not working for me, I use this code and this work :)
int MyRoundedNumber= (int) MyDecimalNumber;
if (Convert.ToInt32(MyDecimalNumber.ToString().Split('.')[1]) != 0)
MyRoundedNumber++;
and if you want to round negative number to down for example round -1.1 to -2 use this
int MyRoundedNumber= (int) MyDecimalNumber;
if (Convert.ToInt32(MyDecimalNumber.ToString().Split('.')[1]) != 0)
if(MyRoundedNumber>=0)
MyRoundedNumber++;
else
MyRoundedNumber--;
var d = 1.5m;
var i = (int)Math.Ceiling(d);
Console.Write(i);