我不熟悉 XNA,但在一个 Silverlight 项目中,我不得不做同样的事情,我最终从上标字符构造科学记数法数字。
您不需要特殊字体,只需要具有下面使用的上标字符的 Unicode 字体。
这是将数字 0-9 映射到相应字符的代码:
private static string GetSuperscript(int digit)
{
switch (digit)
{
case 0:
return "\x2070";
case 1:
return "\x00B9";
case 2:
return "\x00B2";
case 3:
return "\x00B3";
case 4:
return "\x2074";
case 5:
return "\x2075";
case 6:
return "\x2076";
case 7:
return "\x2077";
case 8:
return "\x2078";
case 9:
return "\x2079";
default:
return string.Empty;
}
}
这会将你原来的双精度数转换为科学记数法
public static string FormatAsPowerOfTen(double? value, int decimals)
{
if(!value.HasValue)
{
return string.Empty;
}
var exp = (int)Math.Log10(value.Value);
var fmt = string.Format("{{0:F{0}}}x10{{1}}", decimals);
return string.Format(fmt, value / Math.Pow(10, exp), FormatExponentWithSuperscript(exp));
}
private static string FormatExponentWithSuperscript(int exp)
{
var sb = new StringBuilder();
bool isNegative = false;
if(exp < 0)
{
isNegative = true;
exp = -exp;
}
while (exp != 0)
{
sb.Insert(0, GetSuperscript(exp%10));
exp = exp/10;
}
if(isNegative)
{
sb.Insert(0, "-");
}
return sb.ToString();
}
所以现在您应该可以使用FormatAsPowerOfTen(123400, 2) 生成1.23x10⁵。