/// <summary>
/// 科学计数法值转换成正常值
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
private object ValueScientificNotationConvert(JToken value)
{ if (value == null) return ;
string strData = "";
if (value.Type == JTokenType.String)
{
strData = value.ToString();
}
else
{
strData = value.ToString(Formatting.Indented);
}
Decimal dData = 0.0M;
try
{
if (strData.ToUpper().Contains("E"))
{
System.Globalization.NumberStyles sty = System.Globalization.NumberStyles.AllowExponent | System.Globalization.NumberStyles.AllowDecimalPoint;
try
{
dData = Decimal.Parse(strData, sty);
}
catch (Exception ex)
{
//转换成decimal溢出时,转换成字符串
if (ex is OverflowException)
return ValueScientificNotationConvert1(value);
} }
else
{
if (!decimal.TryParse(strData, out dData))
return null;
}
}
catch (Exception ex)
{
throw ex;
} return dData;
}
/// <summary>
/// 科学计数法值转换成正常值字符串
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public string ValueScientificNotationConvert1(JToken value)
{
if (value == null) return "";
string s = "";
if (value.Type == JTokenType.String)
{
s = value.ToString();
}
else
{
s = value.ToString(Formatting.Indented);
}
string num = String.Empty; string[] arr = s.Split('E');
bool isPart1Double = double.TryParse(arr[], out var part1);
int decimalDigitCountInPart1 = arr[].IndexOf('.') == - ? : arr[].Length - - arr[].IndexOf('.'); string newPart2 = arr.ElementAtOrDefault();
bool isPart2Int = int.TryParse(newPart2, out var part2);
if (arr.Length == )
{
if (isPart1Double)
num = part1.ToString();
}
if (arr.Length == )
{
if (part2 < || part2 - decimalDigitCountInPart1 < )
{
if (isPart1Double && isPart2Int)
num = Decimal.Parse((part1 * Math.Pow(, part2)).ToString(), System.Globalization.NumberStyles.Float).ToString(); // 此时Math.Pow(10, part2)并不会越界,故可使用Decimal.Parse从scientific num转换为real num
}
else if (part2 - decimalDigitCountInPart1 >= )
{
num = part1.ToString().Replace(".", "") + new String('', part2 - decimalDigitCountInPart1 + ); // 避免越界,此处使用字符串拼接代替Math.Pow(10, part2),拼接还可用StringBuilder的append方法 } } return num;
}