How do I convert a string containing a decimal number to nearest integer value?
For example:
string x = "4.72";
I want to get 5 as integer.
How do I convert a string containing a decimal number to nearest integer value?
For example:
string x = "4.72";
I want to get 5 as integer.
It is not clear that either you want to round or get the ceiling value but we have Math
class which provides the basic methods to extract the floor value using Floor()
method or ceiling value using Ceiling()
or rounded value using the Round()
like:
string x = "4.72";
Console.WriteLine(Math.Ceiling(Convert.ToDecimal(x)));
Console.WriteLine(Math.Floor(Convert.ToDecimal(x)));
Console.WriteLine(Math.Round(Convert.ToDecimal(x)));
5
4
5
Please see this working DEMO Fiddle.
Note: for cases where we want the math convention for rounding the decimal points then we should always be calling the Math.Round()
.