格式为C#Double到Scientfic符号的倍数为3的幂 [英] Format C# Double to Scientfic Notation in powers with multiples of three

查看:166
本文介绍了格式为C#Double到Scientfic符号的倍数为3的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用科学的格式格式化一些大数字,但是我需要三倍的倍数。有没有推荐的方法?



我在表中有一系列数字,而不是真正的科学格式(小数点前有一位数字)很高兴有这样的变化,以获得三分之一的权力,例如:



3.123e + 003

19.523 e + 003



而不是:



3.123e + 003

1.952e +004



将所有权力作为三分之一的倍数使我的表格更容易扫描,我相信。



感谢提前

解决方案

我想你需要编写自己的功能。
首先,您可以获得数字的科学表示,其精度比您需要的数字更大。
那么你应该解析生成的字符串,得到浮点数和10的幂指数作为十进制数。之后,您将分区索引的剩余部分分析3,并以适当的方式更改数字。最后你生成输出字符串。

  static string Scientific3(double value,int precision)
{
string fstr1 ={0:E+(precision + 2).ToString()+};
string step1 = string.Format(fstr1,value);
int index1 = step1.ToLower()。IndexOf('e');
if(index1 <0 || index1 == step1.Length - 1)
throw new Exception();
decimal coeff = Convert.ToDecimal(step1.Substring(0,index1));
int index = Convert.ToInt32(step1.Substring(index1 + 1));
while(index%3!= 0)
{
index--;
coeff * = 10;
}
string fstr2 ={0:F+ precision.ToString()+} E {1} {2:D3};
return string.Format(fstr2,coeff,((index< 0)? - :+),Math.Abs​​(index));
}


I'm trying to format some large numbers in scientific format, but I need the power in multiples of three. Is there a recommended way to do this?

I have a range of numbers in a table and instead of true scientific format (with a single digit before the decimal point) I'm happy to have that change in order to have a power with a multiple of three, for example:

3.123e+003
19.523e+003

Rather than:

3.123e+003
1.952e+004

Having all the powers as multiples of three makes my table easier to scan, I believe.

Thanks in advance

解决方案

I think you need to write your own function. At first you can get the scientific representation of the number with the precision two digits larger than you need. Then you should parse resulting string to get floating-point coefficient and 10's power index as numbers of type decimal. After that you analyse the remainder of division index by 3 and change the numbers in the appropriate way. Finally you generate output string.

static string Scientific3(double value, int precision)
{
    string fstr1 = "{0:E" + (precision+2).ToString() + "}";
    string step1 = string.Format(fstr1, value);
    int index1 = step1.ToLower().IndexOf('e');
    if (index1 < 0 || index1 == step1.Length - 1)
        throw new Exception();
    decimal coeff = Convert.ToDecimal(step1.Substring(0, index1));
    int index = Convert.ToInt32(step1.Substring(index1 + 1));
    while(index%3!=0)
    {
        index--;
        coeff *= 10;
    }
    string fstr2 = "{0:F" + precision.ToString() + "}E{1}{2:D3}";
    return string.Format(fstr2, coeff, ((index < 0) ? "-" : "+"), Math.Abs(index));
}

这篇关于格式为C#Double到Scientfic符号的倍数为3的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆