ASP.NET - round off a double value - Asked By anbu n on 03-Nov-11 03:23 AM


the above no needed to round off to 551426


but when is Math.roundoff ..... i get only 551425.45 or 551425
Jitendra Faye replied to anbu n on 03-Nov-11 03:30 AM
Use this code-

double d = 551425.451078772;
int i = (int)Math.Ceiling(d);

Try this and let me know.
dipa ahuja replied to anbu n on 03-Nov-11 03:43 AM

If you want to round to the nearest int:

int rounded = (int)Math.Round(precise, 0);

You can also use:

int rounded = Convert.ToInt32(precise);

Which will use Math.Round(x, 0); to round and cast for you. It looks neater but is slightly less clear IMO.

Reena Jain replied to anbu n on 03-Nov-11 04:26 AM

Here is simple solution for you

decimalVar.ToString ("#.##");//var decimalVar is your variable name

Hope this helps you
Chintan Vaghela replied to anbu n on 03-Nov-11 04:42 AM

try following approach,

string strNumber = "551425.451078772";

strNumber = strNumber.ToString("#.##");

Hope this helpful;
Anoop S replied to anbu n on 03-Nov-11 04:44 AM
Use Math.Ceiling Method, Returns the smallest whole number greater than or equal to the specified number.

Math.Ceiling(551425.451078772) gives 551426