//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
// UserRTX.NET satellite library | Author: Sergei Y.Kitáev
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
using System;

#if !WASM
namespace user_rtx { using u = user_rtx;
#else
namespace user_rtx.wasm { using u = user_rtx.wasm;
#endif

public static class percent {
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="DivideByZeroException" />
/// <exception cref="OverflowException" />
public static int FromScale( double value ) =>
   FromScale( value, 1.0 );

/// <exception cref="ArgumentOutOfRangeException" />
/// <exception cref="DivideByZeroException" />
/// <exception cref="OverflowException" />
public static int FromScale( double value, double entireValue )
{
   math_argcheck.Number( value, nameof(value) );
   math_argcheck.Number( entireValue, nameof(entireValue) );
   if ( entireValue == 0 )
       throw new DivideByZeroException();
   try { return (int)Math.Round( value / entireValue * 100 ); }
   catch { throw new OverflowException(); }
}

//--------------------------------------------------------------------------------------------------

public static double ToDoubleScale( double percent ) =>
   ToDoubleScale( percent, 1.0 );
public static double ToDoubleScale( double percent, double entireValue ) =>
   percent / 100 * entireValue;

public static int IntRound( double percent ) =>
   IntRound( percent, 1.0 );
public static int IntRound( double percent, double entireValue ) =>
   math.IntRound( ToDoubleScale( percent, entireValue ) );

//==================================================================================================

/// <exception cref="ArgumentException" />
/// <exception cref="DivideByZeroException" />
/// <exception cref="OverflowException" />
public static int FromScale( decimal value ) =>
   FromScale( (double)value );

/// <exception cref="ArgumentException" />
/// <exception cref="DivideByZeroException" />
/// <exception cref="OverflowException" />
public static int FromScale( decimal value, decimal entireValue ) =>
   FromScale( (double)value, (double)entireValue );

//--------------------------------------------------------------------------------------------------

public static decimal ToDecimalScale( decimal percent ) =>
   (decimal)ToDoubleScale( (double)percent );
public static decimal ToDecimalScale( decimal percent, decimal entireValue ) =>
   (decimal)ToDoubleScale( (double)percent, (double)entireValue );

//--------------------------------------------------------------------------------------------------

public static int IntRound( decimal percent ) =>
   IntRound( (double)percent );
public static int IntRound( decimal percent, decimal entireValue ) =>
   IntRound( (double)percent, (double)entireValue );

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
} // percent

} // user_rtx (.wasm)
