Limiting doubles in C#

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.