[Benchmark]
public static string NoCache2()
{
return ToPercentageString(0.0433m, 2);
}
[Benchmark]
public static string NoCache3()
{
return ToPercentageString(0.0433m, 3);
}
[Benchmark]
public static string Cache2()
{
return ToPercentageString2Dp(0.0433m);
}
[Benchmark]
public static string Cache3()
{
return ToPercentageString3Dp(0.0433m);
}
public static string ToPercentageString(decimal d, int decimalPlaces)
=> d.ToString(
"P",
new NumberFormatInfo()
{
PercentDecimalDigits = decimalPlaces,
PercentPositivePattern = 1
}
);
public static string ToPercentageString2Dp(decimal d)
=> d.ToString("P", TwoDpFormat);
public static string ToPercentageString3Dp(decimal d)
=> d.ToString("P", ThreeDpFormat);
private static readonly NumberFormatInfo TwoDpFormat = new NumberFormatInfo()
{
PercentDecimalDigits = 2,
PercentPositivePattern = 1
};
private static readonly NumberFormatInfo ThreeDpFormat = new NumberFormatInfo()
{
PercentDecimalDigits = 3,
PercentPositivePattern = 1
};
[Benchmark]
public static string NoCache2()
{
return ToPercentageString(0.0433m, 2);
}
[Benchmark]
public static string NoCache3()
{
return ToPercentageString(0.0433m, 3);
}
[Benchmark]
public static string Cache2()
{
return ToPercentageString2Dp(0.0433m);
}
[Benchmark]
public static string Cache3()
{
return ToPercentageString3Dp(0.0433m);
}
public static string ToPercentageString(decimal d, int decimalPlaces)
=> d.ToString(
"P",
new NumberFormatInfo()
{
PercentDecimalDigits = decimalPlaces,
PercentPositivePattern = 1
}
);
public static string ToPercentageString2Dp(decimal d)
=> d.ToString("P", TwoDpFormat);
public static string ToPercentageString3Dp(decimal d)
=> d.ToString("P", ThreeDpFormat);
private static readonly NumberFormatInfo TwoDpFormat = new NumberFormatInfo()
{
PercentDecimalDigits = 2,
PercentPositivePattern = 1
};
private static readonly NumberFormatInfo ThreeDpFormat = new NumberFormatInfo()
{
PercentDecimalDigits = 3,
PercentPositivePattern = 1
};