172 lines
5.3 KiB
C#
172 lines
5.3 KiB
C#
using Newtonsoft.Json;
|
|
using System.Runtime.CompilerServices;
|
|
using YLErp.Models;
|
|
|
|
namespace YLErp.Commons
|
|
{
|
|
/// <summary>
|
|
/// OTC Format
|
|
/// </summary>
|
|
public static class OtcFormatHelper
|
|
{
|
|
static OtcFormatModel _formatModel;
|
|
|
|
public static OtcFormatModel FormatModel
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
get { return _formatModel ?? (_formatModel = new OtcFormatModel()); }
|
|
}
|
|
|
|
public static void Initialize(string json)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(json))
|
|
{
|
|
_formatModel = JsonConvert.DeserializeObject<OtcFormatModel>(json);
|
|
}
|
|
}
|
|
|
|
#region----FormatValue----
|
|
|
|
/// <summary>
|
|
/// 四舍五入小数位
|
|
/// </summary>
|
|
public static double FormatValue(this double d, int precision, bool rounded = true)
|
|
{
|
|
if (double.IsNaN(d))
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
if (rounded)
|
|
{
|
|
return Math.Round(d, precision > 0 ? precision : 0, MidpointRounding.AwayFromZero);
|
|
}
|
|
|
|
if (precision > 0)
|
|
{
|
|
var pow = Math.Pow(10, precision);
|
|
return Math.Truncate(d * pow) / pow;
|
|
}
|
|
|
|
return Math.Truncate(d);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 四舍五入小数位
|
|
/// </summary>
|
|
public static decimal FormatValue(decimal d, int precision, bool rounded = true)
|
|
{
|
|
if (rounded)
|
|
{
|
|
return Math.Round(d, precision > 0 ? precision : 0, MidpointRounding.AwayFromZero);
|
|
}
|
|
|
|
if (precision > 0)
|
|
{
|
|
var pow = Convert.ToDecimal(Math.Pow(10, precision));
|
|
return Math.Truncate(d * pow) / pow;
|
|
}
|
|
|
|
return Math.Truncate(d);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 四舍五入小数位
|
|
/// </summary>
|
|
public static double? FormatValue(this double? d, int precision, bool rounded = true)
|
|
{
|
|
return d.HasValue ? FormatValue(d.Value, precision, rounded) : d;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 四舍五入小数位
|
|
/// </summary>
|
|
public static decimal? FormatValue(decimal? d, int precision, bool rounded = true)
|
|
{
|
|
return d.HasValue ? FormatValue(d.Value, precision, rounded) : d;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region----OtcFormat----
|
|
|
|
/// <summary>
|
|
/// 获取期权费总额格式转换
|
|
/// </summary>
|
|
public static double GetTradePriceDouble(double value)
|
|
{
|
|
return FormatModel.trading.tradePrice.FormatValue(value);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 格式化权利金或补偿支付(百分数%和绝对值)
|
|
/// </summary>
|
|
public static string FormatPremium(bool? IsUsePremiumRate, double? percentValue, double? absValue)
|
|
{
|
|
return IsUsePremiumRate == true ? percentValue.OtcFormat(OtcFormatFlag.premiumRateP) : absValue.OtcFormat(OtcFormatFlag.tradeSinglePrice);
|
|
}
|
|
|
|
/// <summary>
|
|
/// OtcFormat扩展
|
|
/// </summary>
|
|
public static int GetMaxPrecision(OtcFormatFlag fmt)
|
|
{
|
|
switch (fmt)
|
|
{
|
|
case OtcFormatFlag.umprice:
|
|
return FormatModel.trading.umprice.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.umpriceP:
|
|
return FormatModel.trading.umpriceP.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.umpricePR:
|
|
return FormatModel.trading.umpricePR.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.tradePrice:
|
|
return FormatModel.trading.tradePrice.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.tradeSinglePrice:
|
|
return FormatModel.trading.tradeSinglePrice.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.StockEqvNotional:
|
|
return FormatModel.trading.StockEqvNotional.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.premiumRate:
|
|
return FormatModel.trading.premiumRate.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.premiumRateP:
|
|
return FormatModel.trading.premiumRateP.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.notional:
|
|
return FormatModel.trading.notional.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.notionalP:
|
|
return FormatModel.trading.notionalP.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.greek:
|
|
return FormatModel.trading.greek.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.percent: return 2;
|
|
|
|
case OtcFormatFlag.percent4: return 4;
|
|
|
|
case OtcFormatFlag.volatility:
|
|
return FormatModel.trading.volatility.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.volatilityP:
|
|
return FormatModel.trading.volatilityP.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.marginRate:
|
|
return FormatModel.trading.marginRate.GetMaxPrecision();
|
|
|
|
case OtcFormatFlag.marginRateP:
|
|
return FormatModel.trading.marginRateP.GetMaxPrecision();
|
|
|
|
default: return 2;
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
}
|