Round Off decimal values in C#
Look at Math.Round(decimal)
or the overload which takes a MidpointRounding
argument.
Of course, you'll need to parse and format the value to get it from/to text. If this is input entered by the user, you should probably use decimal.TryParse
, using the return value to determine whether or not the input was valid.
string text = "19500.55";
decimal value;
if (decimal.TryParse(text, out value))
{
value = Math.Round(value);
text = value.ToString();
// Do something with the new text value
}
else
{
// Tell the user their input is invalid
}
Math.Round( value, 0 )