//http://advancementvoyage.codeplex.com/
//Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AdvancementVoyage.Magic.Utility
{
/// <summary>
/// Extension methods for the int type.
/// </summary>
public static class IntExtensions
{
/// <summary>
/// Converts an integer into a roman numeral.
/// </summary>
/// <param name="number">
/// The number being transformed.
/// </param>
/// <returns>
/// A string representation of the number's corresponding roman numeral.
/// </returns>
public static string ToRomanNumeral(this int number)
{
var retVal = new StringBuilder(5);
var valueMap = new SortedDictionary<int, string>
{
{ 1, "I" },
{ 4, "IV" },
{ 5, "V" },
{ 9, "IX" },
{ 10, "X" },
{ 40, "XL" },
{ 50, "L" },
{ 90, "XC" },
{ 100, "C" },
{ 400, "CD" },
{ 500, "D" },
{ 900, "CM" },
{ 1000, "M" },
};
foreach (var kvp in valueMap.Reverse())
{
while (number >= kvp.Key)
{
number -= kvp.Key;
retVal.Append(kvp.Value);
}
}
return retVal.ToString();
}
}
}
Month: February 2011
Int extension Convert integer to boolean, is it even, is it positive
//http://advancementvoyage.codeplex.com/
//Microsoft Public License (Ms-PL)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AroLibraries.ExtensionMethods
{
public static class IntExt
{
public static IEnumerable<int> Ext_IterateTo(this int start, int end)
{
var diff = end - start > 0 ? 1 : -1;
for (var current = start; current != end; current += diff)
{
yield return current;
}
}
public static bool Ext_ToBool(this int i)
{
if (i > 0)
return true;
return false;
}
public static bool Ext_IsEven(this int iInt)
{
if ((iInt % 2) == 0)
{
return true;
}
return false;
}
public static bool Ext_IsPositive(this int iInt)
{
if (iInt > 0)
return true;
return false;
}
}
}
Calculate average for a list of integer values with params int[] values
class TestParams
{
private static void Average(string title, params int[] values)
{
int sum = 0;
for (int i = 0; i < values.Length; i++)
{
sum += values[i];
System.Console.Write(values[i] + ", ");
}
System.Console.WriteLine((float)sum/values.Length);
}
static void Main()
{
Average ("List One", 5, 10, 15);
Average ("List Two", 5, 10, 15, 20, 25, 30);
}
}
[/csharp]
Write Synch safe Int32
//http://walkmen.codeplex.com/license
// License: GNU General Public License version 2 (GPLv2)
using System;
using System.Collections.Generic;
using System.Text;
namespace Walkmen.Util
{
public sealed class NumericUtils
{
private NumericUtils()
{
}
public static byte[] WriteSynchsafeInt32(int value)
{
byte[] result = new byte[4];
result[0] = (byte)((value & 0xFE00000) >> 21);
result[1] = (byte)((value & 0x01FC000) >> 14);
result[2] = (byte)((value & 0x0003F80) >> 7);
result[3] = (byte)((value & 0x000007F));
return result;
}
}
}
Read sync safe int 32
//http://walkmen.codeplex.com/license
// License: GNU General Public License version 2 (GPLv2)
using System;
using System.Collections.Generic;
using System.Text;
namespace Walkmen.Util
{
public sealed class NumericUtils
{
private NumericUtils()
{
}
public static int ReadSynchsafeInt32(byte[] buffer, int offset)
{
return ((buffer[offset + 0] & 0x7F) << 21) | ((buffer[offset + 1] & 0x7F) << 14) | ((buffer[offset + 2] & 0x7F) << 7) | (buffer[offset + 3] & 0x7f);
}
}
}
[/csharp]
Max/Min for int and double values
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal class MathUtils
{
public static int? Min(int? val1, int? val2)
{
if (val1 == null)
return val2;
if (val2 == null)
return val1;
return Math.Min(val1.Value, val2.Value);
}
public static int? Max(int? val1, int? val2)
{
if (val1 == null)
return val2;
if (val2 == null)
return val1;
return Math.Max(val1.Value, val2.Value);
}
public static double? Min(double? val1, double? val2)
{
if (val1 == null)
return val2;
if (val2 == null)
return val1;
return Math.Min(val1.Value, val2.Value);
}
public static double? Max(double? val1, double? val2)
{
if (val1 == null)
return val2;
if (val2 == null)
return val1;
return Math.Max(val1.Value, val2.Value);
}
}
}
Get the digit-length of an int value
#region License
// Copyright (c) 2007 James Newton-King
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the “Software”), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Collections.Generic;
using System.Text;
namespace Newtonsoft.Json.Utilities
{
internal class MathUtils
{
public static int IntLength(int i)
{
if (i < 0)
throw new ArgumentOutOfRangeException();
if (i == 0)
return 1;
return (int)Math.Floor(Math.Log10(i)) + 1;
}
}
}
[/csharp]

