Innovenergy_trunk/csharp/Lib/Utils/DecimalUtils.cs

32 lines
837 B
C#
Raw Normal View History

using DecimalMath;
namespace InnovEnergy.Lib.Utils;
public static class DecimalUtils
{
public static Double RoundToSignificantFigures(this Double num, Int32 n)
{
if (num == 0)
return 0;
var d = Math.Ceiling(Math.Log10(num < 0 ? -num : num));
var power = n - (Int32)d;
var magnitude = Math.Pow(10, power);
var shifted = Math.Round(num * magnitude);
return shifted / magnitude;
}
public static Decimal RoundToSignificantFigures(this Decimal num, Int32 n)
{
if (num == 0)
return 0;
var d = Math.Ceiling(DecimalEx.Log10(num < 0 ? -num : num));
var power = n - (Int32)d;
var magnitude = DecimalEx.Pow(10, power);
var shifted = Math.Round(num * magnitude);
return shifted / magnitude;
}
}