402 lines
18 KiB
C#
402 lines
18 KiB
C#
using YLErp.Commons;
|
|
using static YLErp.Models.OtcFormatModel;
|
|
|
|
namespace YLErp.BasicTests
|
|
{
|
|
[TestClass]
|
|
public class OtcFormatTest
|
|
{
|
|
[TestMethod("OtcFormatOption")]
|
|
public void TestOtcFormatOption()
|
|
{
|
|
const double d1 = 2221585d;
|
|
const double d2 = 2221585.99665655;
|
|
|
|
//-------------------------------------
|
|
|
|
var option = new OtcFormatOption { };
|
|
Assert.AreEqual(option.GetFormat(), "0");
|
|
Assert.AreEqual(option.Format(d1), "2221585");
|
|
Assert.AreEqual(option.Format(d2), "2221586");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { grouping = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0");
|
|
Assert.AreEqual(option.Format(d1), "2,221,585");
|
|
Assert.AreEqual(option.Format(d2), "2,221,586");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { rounded = false };
|
|
Assert.AreEqual(option.GetFormat(), "0");
|
|
Assert.AreEqual(option.Format(d1), "2221585");
|
|
Assert.AreEqual(option.Format(d2), "2221585");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { percent = true };
|
|
Assert.AreEqual(option.GetFormat(), "0%");
|
|
Assert.AreEqual(option.Format(d1), "222158500%");
|
|
Assert.AreEqual(option.Format(d2), "222158600%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { grouping = true, rounded = false };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0");
|
|
Assert.AreEqual(option.Format(d1), "2,221,585");
|
|
Assert.AreEqual(option.Format(d2), "2,221,585");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { grouping = true, percent = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0%");
|
|
Assert.AreEqual(option.Format(d1), "222,158,500%");
|
|
Assert.AreEqual(option.Format(d2), "222,158,600%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { grouping = true, percent = true, rounded = false };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0%");
|
|
Assert.AreEqual(option.Format(d1), "222,158,500%");
|
|
Assert.AreEqual(option.Format(d2), "222,158,599%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2 };
|
|
Assert.AreEqual(option.GetFormat(), "0.00");
|
|
Assert.AreEqual(option.Format(d1), "2221585.00");
|
|
Assert.AreEqual(option.Format(d2), "2221586.00");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 0, maxDecimals = 4 };
|
|
Assert.AreEqual(option.GetFormat(), "0.####");
|
|
Assert.AreEqual(option.Format(d1), "2221585");
|
|
Assert.AreEqual(option.Format(d2), "2221585.9967");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 0, maxDecimals = 4, rounded = false };
|
|
Assert.AreEqual(option.GetFormat(), "0.####");
|
|
Assert.AreEqual(option.Format(d1), "2221585");
|
|
Assert.AreEqual(option.Format(d2), "2221585.9966");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 0, maxDecimals = 4, grouping = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0.####");
|
|
Assert.AreEqual(option.Format(d1), "2,221,585");
|
|
Assert.AreEqual(option.Format(d2), "2,221,585.9967");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 0, maxDecimals = 4, grouping = true, percent = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0.####%");
|
|
Assert.AreEqual(option.Format(d1), "222,158,500%");
|
|
Assert.AreEqual(option.Format(d2), "222,158,599.6657%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 0, maxDecimals = 4, grouping = true, percent = true, rounded = false };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0.####%");
|
|
Assert.AreEqual(option.Format(d1), "222,158,500%");
|
|
Assert.AreEqual(option.Format(d2), "222,158,599.6656%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2, maxDecimals = 4 };
|
|
Assert.AreEqual(option.GetFormat(), "0.00##");
|
|
Assert.AreEqual(option.Format(d1), "2221585.00");
|
|
Assert.AreEqual(option.Format(d2), "2221585.9967");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2, maxDecimals = 4, rounded = false };
|
|
Assert.AreEqual(option.GetFormat(), "0.00##");
|
|
Assert.AreEqual(option.Format(d1), "2221585.00");
|
|
Assert.AreEqual(option.Format(d2), "2221585.9966");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2, maxDecimals = 4, rounded = false, percent = true };
|
|
Assert.AreEqual(option.GetFormat(), "0.00##%");
|
|
Assert.AreEqual(option.Format(d1), "222158500.00%");
|
|
Assert.AreEqual(option.Format(d2), "222158599.6656%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2, maxDecimals = 4, rounded = false, grouping = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0.00##");
|
|
Assert.AreEqual(option.Format(d1), "2,221,585.00");
|
|
Assert.AreEqual(option.Format(d2), "2,221,585.9966");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2, maxDecimals = 4, rounded = true, grouping = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0.00##");
|
|
Assert.AreEqual(option.Format(d1), "2,221,585.00");
|
|
Assert.AreEqual(option.Format(d2), "2,221,585.9967");
|
|
|
|
//---------------------------------------------------
|
|
|
|
option = new OtcFormatOption { minDecimals = 2, maxDecimals = 4, rounded = true, grouping = true, percent = true };
|
|
Assert.AreEqual(option.GetFormat(), "#,##0.00##%");
|
|
Assert.AreEqual(option.Format(d1), "222,158,500.00%");
|
|
Assert.AreEqual(option.Format(d2), "222,158,599.6657%");
|
|
|
|
//---------------------------------------------------
|
|
|
|
Assert.AreEqual(10d.OtcFormatFlex(0, 10), "10");
|
|
Assert.AreEqual(10.1d.OtcFormatFlex(0, 10), "10.1");
|
|
Assert.AreEqual(10.11d.OtcFormatFlex(0, 10), "10.11");
|
|
Assert.AreEqual(10.123d.OtcFormatFlex(0, 10), "10.123");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OtcFormatTest1()
|
|
{
|
|
var model = new OtcFormatModel
|
|
{
|
|
trading = new TradingFormat
|
|
{
|
|
premiumRateP = new OtcFormatOptionPercent { minDecimals = 2, maxDecimals = 4 }
|
|
}
|
|
};
|
|
OtcFormatHelper.Initialize(JsonHelper.Serialize(model));
|
|
Assert.AreEqual(OtcFormatExtensions.OtcFormat(0.22556677, OtcFormatFlag.premiumRate), "0.225567");
|
|
Assert.AreEqual(OtcFormatExtensions.OtcFormat(0.22556677, OtcFormatFlag.premiumRateP), "22.5567%");
|
|
|
|
Assert.AreEqual(OtcFormatExtensions.OtcFormatMoney(2255.667788, grouping: true), "2,255.67");
|
|
Assert.AreEqual(OtcFormatExtensions.OtcFormatMoney(2255.667788, grouping: false), "2255.67");
|
|
}
|
|
|
|
[TestMethod]
|
|
public void OtcFormatTest2()
|
|
{
|
|
var d = 0.223;
|
|
var a1 = d.ToString(GetUmPriceFormat() + "%");
|
|
var a2 = d.OtcFormatUmPrice(true);
|
|
Assert.AreEqual(a1, "22.3000%");
|
|
Assert.AreEqual(a2, "22.30%");
|
|
|
|
var PremiumRate = 0.225566778899;
|
|
var fmt = GetPremiumRateFormat() + "%";
|
|
var str1 = PremiumRate.ToString(fmt);
|
|
Assert.AreEqual(str1, "22.5567%");
|
|
Assert.AreEqual(str1, PremiumRate.OtcFormat(OtcFormatFlag.premiumRateP));
|
|
}
|
|
|
|
[TestMethod("OtcFormatPercent")]
|
|
public void TestOtcFormatPercent()
|
|
{
|
|
var dnan = double.NaN;
|
|
Assert.AreEqual(dnan.OtcFormatPercent(5), "");
|
|
dnan = double.NegativeInfinity;
|
|
Assert.AreEqual(dnan.OtcFormatPercent(5), "");
|
|
|
|
var d = 0.2233445566778899;
|
|
Assert.AreEqual(d.OtcFormatPercent(0), "22%");
|
|
Assert.AreEqual(d.OtcFormatPercent(1), "22.3%");
|
|
Assert.AreEqual(d.OtcFormatPercent(2), "22.33%");
|
|
Assert.AreEqual(d.OtcFormatPercent(3), "22.334%");
|
|
Assert.AreEqual(d.OtcFormatPercent(4), "22.3345%");
|
|
Assert.AreEqual(d.OtcFormatPercent(5), "22.33446%");
|
|
Assert.AreEqual(d.OtcFormatPercent(6), "22.334456%");
|
|
Assert.AreEqual(d.OtcFormatPercent(7), "22.3344557%");
|
|
Assert.AreEqual(d.OtcFormatPercent(8), "22.33445567%");
|
|
Assert.AreEqual(d.OtcFormatPercent(9), "22.334455668%");
|
|
Assert.AreEqual(d.OtcFormatPercent(10), "22.3344556678%");
|
|
Assert.AreEqual(d.OtcFormatPercent(11), "22.33445566779%");
|
|
Assert.AreEqual(d.OtcFormatPercent(12), "22.334455667789%");
|
|
|
|
d = 22.5566778899;
|
|
Assert.AreEqual(d.OtcFormatPercent(0), "2256%");
|
|
Assert.AreEqual(d.OtcFormatPercent(1), "2255.7%");
|
|
Assert.AreEqual(d.OtcFormatPercent(2), "2255.67%");
|
|
Assert.AreEqual(d.OtcFormatPercent(3), "2255.668%");
|
|
Assert.AreEqual(d.OtcFormatPercent(4), "2255.6678%");
|
|
Assert.AreEqual(d.OtcFormatPercent(5), "2255.66779%");
|
|
Assert.AreEqual(d.OtcFormatPercent(6), "2255.667789%");
|
|
Assert.AreEqual(d.OtcFormatPercent(7), "2255.6677890%");
|
|
Assert.AreEqual(d.OtcFormatPercent(8), "2255.66778899%");
|
|
|
|
Assert.AreEqual(d.OtcFormatPercent(0, grouping: true), "2,256%");
|
|
Assert.AreEqual(d.OtcFormatPercent(1, grouping: true), "2,255.7%");
|
|
Assert.AreEqual(d.OtcFormatPercent(2, grouping: true), "2,255.67%");
|
|
Assert.AreEqual(d.OtcFormatPercent(3, grouping: true), "2,255.668%");
|
|
Assert.AreEqual(d.OtcFormatPercent(4, grouping: true), "2,255.6678%");
|
|
Assert.AreEqual(d.OtcFormatPercent(5, grouping: true), "2,255.66779%");
|
|
Assert.AreEqual(d.OtcFormatPercent(6, grouping: true), "2,255.667789%");
|
|
Assert.AreEqual(d.OtcFormatPercent(7, grouping: true), "2,255.6677890%");
|
|
Assert.AreEqual(d.OtcFormatPercent(8, grouping: true), "2,255.66778899%");
|
|
|
|
double? d2 = default;
|
|
Assert.AreEqual(d2.OtcFormatPercent(0), "");
|
|
Assert.AreEqual(d2.OtcFormatPercent(1), "");
|
|
Assert.AreEqual(d2.OtcFormatPercent(2), "");
|
|
Assert.AreEqual(d2.OtcFormatPercent(3), "");
|
|
|
|
d2 = 22.5566778899;
|
|
Assert.AreEqual(d2.OtcFormatPercent(0), "2256%");
|
|
Assert.AreEqual(d2.OtcFormatPercent(1), "2255.7%");
|
|
Assert.AreEqual(d2.OtcFormatPercent(2), "2255.67%");
|
|
Assert.AreEqual(d2.OtcFormatPercent(3), "2255.668%");
|
|
Assert.AreEqual(d2.OtcFormatPercent(4), "2255.6678%");
|
|
}
|
|
|
|
[TestMethod("OtcFormatFlex")]
|
|
public void TestOtcFormatFlex()
|
|
{
|
|
var dnan = double.NaN;
|
|
Assert.AreEqual(dnan.OtcFormatFlex(5), "");
|
|
dnan = double.NegativeInfinity;
|
|
Assert.AreEqual(dnan.OtcFormatFlex(5), "");
|
|
|
|
var d1 = 22.55;
|
|
Assert.AreEqual(d1.OtcFormatFlex(0), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(0, maxDecimals: 4), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(0, rounded: false), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(0, maxDecimals: 4, rounded: false), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(2), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(2, maxDecimals: 4), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(2, rounded: false), "22.55");
|
|
Assert.AreEqual(d1.OtcFormatFlex(2, maxDecimals: 4, rounded: false), "22.55");
|
|
|
|
var d = 22.5566778899;
|
|
Assert.AreEqual(d.OtcFormatFlex(0), "22.556678");
|
|
Assert.AreEqual(d.OtcFormatFlex(0, maxDecimals: 4), "22.5567");
|
|
Assert.AreEqual(d.OtcFormatFlex(0, rounded: false), "22.556677");
|
|
Assert.AreEqual(d.OtcFormatFlex(0, maxDecimals: 4, rounded: false), "22.5566");
|
|
Assert.AreEqual(d.OtcFormatFlex(2), "22.556678");
|
|
Assert.AreEqual(d.OtcFormatFlex(2, maxDecimals: 4), "22.5567");
|
|
Assert.AreEqual(d.OtcFormatFlex(2, rounded: false), "22.556677");
|
|
Assert.AreEqual(d.OtcFormatFlex(2, maxDecimals: 4, rounded: false), "22.5566");
|
|
|
|
Assert.AreEqual(d.OtcFormatFlex(0, 0, percent: true), "2256%");
|
|
Assert.AreEqual(d.OtcFormatFlex(1, 1, percent: true), "2255.7%");
|
|
Assert.AreEqual(d.OtcFormatFlex(2, 2, percent: true), "2255.67%");
|
|
Assert.AreEqual(d.OtcFormatFlex(3, 3, percent: true), "2255.668%");
|
|
Assert.AreEqual(d.OtcFormatFlex(4, 4, percent: true), "2255.6678%");
|
|
Assert.AreEqual(d.OtcFormatFlex(5, 5, percent: true), "2255.66779%");
|
|
Assert.AreEqual(d.OtcFormatFlex(6, 6, percent: true), "2255.667789%");
|
|
Assert.AreEqual(d.OtcFormatFlex(7, 7, percent: true), "2255.6677890%");
|
|
Assert.AreEqual(d.OtcFormatFlex(8, 8, percent: true), "2255.66778899%");
|
|
|
|
Assert.AreEqual(d.OtcFormatFlex(0, 0, rounded: false, percent: true), "2255%");
|
|
Assert.AreEqual(d.OtcFormatFlex(1, 1, rounded: false, percent: true), "2255.6%");
|
|
Assert.AreEqual(d.OtcFormatFlex(2, 2, rounded: false, percent: true), "2255.66%");
|
|
Assert.AreEqual(d.OtcFormatFlex(3, 3, rounded: false, percent: true), "2255.667%");
|
|
Assert.AreEqual(d.OtcFormatFlex(4, 4, rounded: false, percent: true), "2255.6677%");
|
|
Assert.AreEqual(d.OtcFormatFlex(5, 5, rounded: false, percent: true), "2255.66778%");
|
|
Assert.AreEqual(d.OtcFormatFlex(6, 6, rounded: false, percent: true), "2255.667788%");
|
|
Assert.AreEqual(d.OtcFormatFlex(7, 7, rounded: false, percent: true), "2255.6677889%");
|
|
Assert.AreEqual(d.OtcFormatFlex(8, 8, rounded: false, percent: true), "2255.66778899%");
|
|
|
|
double? dnan2 = double.NaN;
|
|
Assert.AreEqual(dnan2.OtcFormatFlex(5), "");
|
|
dnan2 = double.NegativeInfinity;
|
|
Assert.AreEqual(dnan2.OtcFormatFlex(5), "");
|
|
|
|
double? d2 = default;
|
|
Assert.AreEqual(d2.OtcFormatFlex(0, 0, percent: true), "");
|
|
Assert.AreEqual(d2.OtcFormatFlex(1, 1, percent: true), "");
|
|
Assert.AreEqual(d2.OtcFormatFlex(2, 2, percent: true), "");
|
|
Assert.AreEqual(d2.OtcFormatFlex(3, 3, percent: true), "");
|
|
|
|
d2 = 22.5566778899;
|
|
Assert.AreEqual(d2.OtcFormatFlex(0, 0, percent: true), "2256%");
|
|
Assert.AreEqual(d2.OtcFormatFlex(1, 1, percent: true), "2255.7%");
|
|
Assert.AreEqual(d2.OtcFormatFlex(2, 2, percent: true), "2255.67%");
|
|
Assert.AreEqual(d2.OtcFormatFlex(3, 3, percent: true), "2255.668%");
|
|
Assert.AreEqual(d2.OtcFormatFlex(4, 4, percent: true), "2255.6678%");
|
|
}
|
|
|
|
private static TradingFormat trading => OtcFormatHelper.FormatModel.trading;
|
|
|
|
/// <summary>
|
|
/// 获取标的价格显示格式
|
|
/// </summary>
|
|
public static string GetUmPriceFormat()
|
|
{
|
|
if (trading.umprice.precision <= 0)
|
|
{
|
|
trading.umprice.precision = 4;
|
|
}
|
|
return "0.".PadRight(trading.umprice.precision + 2, '0');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取期权费单价显示格式
|
|
/// </summary>
|
|
public static string GetTradeSinglePriceFormat()
|
|
{
|
|
if (trading.tradeSinglePrice.precision <= 0)
|
|
{
|
|
trading.tradeSinglePrice.precision = 3;
|
|
}
|
|
return "0.".PadRight(trading.tradeSinglePrice.precision + 2, '0');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取名义本金显示格式
|
|
/// </summary>
|
|
public static string GetStockEqvNotionalFormat()
|
|
{
|
|
if (trading.StockEqvNotional.precision <= 0)
|
|
{
|
|
trading.StockEqvNotional.precision = 2;
|
|
}
|
|
return "0.".PadRight(trading.StockEqvNotional.precision + 2, '0');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取期权费总额显示格式
|
|
/// </summary>
|
|
public static string GetTradePriceFormat()
|
|
{
|
|
return "0.00";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取期权费率显示格式(percentFmt为true时返回P格式)
|
|
/// </summary>
|
|
public static string GetPremiumRateFormat(bool percentFmt = false)
|
|
{
|
|
if (percentFmt)
|
|
{
|
|
return trading.premiumRateP.precision < 1 ? "P4" : "P" + trading.premiumRateP;
|
|
}
|
|
if (trading.premiumRateP.precision <= 0)
|
|
{
|
|
trading.premiumRateP.precision = 4;
|
|
}
|
|
return "0.".PadRight(trading.premiumRateP.precision + 2, '0');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取希腊值显示格式
|
|
/// </summary>
|
|
public static string GetGreekFormat()
|
|
{
|
|
if (trading.greek.precision <= 0)
|
|
{
|
|
trading.greek.precision = 3;
|
|
}
|
|
return "0.".PadRight(trading.greek.precision + 2, '0');
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数量显示格式
|
|
/// </summary>
|
|
public static string GetNotional()
|
|
{
|
|
if (trading.notional.precision <= 0)
|
|
{
|
|
trading.notional.precision = 2;
|
|
}
|
|
return "0.".PadRight(trading.notional.precision + 2, '0');
|
|
}
|
|
}
|
|
}
|