Limiting doubles in C#
July 29, 2010 Leave a Comment
I have had a reason to limit the number of decimal places of a double to store in a database. After a quick scout through the web I came across the answer using the Math class. Rather than explain, here is a method showing the use of the Math.Round method.
public double PixelPositionToPercentOfHeight(int top)
{
double percentage = (double)top / (double)(this.ClientRectangle.Height);
// Limit to 6 decimal places
return Math.Round(percentage, 6);
}
Advertisement