Mercurial > repos > IBBoard
changeset 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 | 966ba575d4e6 |
children | a177a3750acd |
files | IBBoard.csproj Limits/SimpleRoundedPercentageLimit.cs |
diffstat | 2 files changed, 39 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/IBBoard.csproj Tue Oct 06 18:49:04 2009 +0000 +++ b/IBBoard.csproj Tue Oct 06 18:59:25 2009 +0000 @@ -129,6 +129,7 @@ <Compile Include="Lang\IBBMath.cs" /> <Compile Include="CustomMath\IBBMath.cs" /> <Compile Include="Limits\NumericSizeConstrainedLimit.cs" /> + <Compile Include="Limits\SimpleRoundedPercentageLimit.cs" /> </ItemGroup> <ItemGroup> <Content Include="libs\log4net.dll" />
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Limits/SimpleRoundedPercentageLimit.cs Tue Oct 06 18:59:25 2009 +0000 @@ -0,0 +1,38 @@ +// 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); + } + } +}