view Limits/SimpleRoundedPercentageLimit.cs @ 48:f3bb5b77a7e4

Re #24: Add "limit" objects that can be used for numeric limits * Add simple rounded limit
author IBBoard <dev@ibboard.co.uk>
date Tue, 06 Oct 2009 18:59:25 +0000
parents
children a177a3750acd
line wrap: on
line source

// This file (SimpleRoundedPercentageLimit.cs) is a part of the IBBoard project and is copyright 2009 IBBoard
// 
// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.

using System;
using IBBoard.CustomMath;

namespace IBBoard
{
	/// <summary>
	/// A percentage-based limit that always either rounds up or down to the closest integer
	/// </summary>
	public class SimpleRoundedPercentageLimit
	{
		private double limit;
		private bool roundUp;
		
		public SimpleRoundedPercentageLimit (double percentageLimit, bool roundFractionUp)
		{
			limit = percentageLimit;
			roundUp = roundFractionUp;
		}
		
		/// <summary>
		/// Gets the limited number, based on the percentage limit that this <code>Limit</code> represents and the rounding direction
		/// </summary>
		/// <param name="size">
		/// The maximum size
		/// </param>
		/// <returns>
		/// <code>size</code> or the numeric limit this object was created with, whichever is smaller.
		/// </returns>
		public int GetLimit(int size)
		{
			return (int)IBBMath.Round(size * limit / 100, roundUp);
		}
	}
}