如何将以16为底的数字转换为以10为底的数字. [英] how to convert digit in 16 base to base 10.

查看:194
本文介绍了如何将以16为底的数字转换为以10为底的数字.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布了一些有关PI和C#数学的问题,我几乎在那里,但是我需要问最后一个问题.

我想出了如何计算PI的方法,只是意识到这不是我所需要的,只能做到这一点.然后我发现您可以使用BBP公式将PI计算为您选择的位数.

我的问题是BBP以16为基数,我需要以10为基数的这个数字.
我当前的代码如下:

I have posted a few questions lately about PI, and C# math, and i almost there, but i need to ask one last question.

I figured out how to calculate PI, only to realize this is not what i needed for you can only go so far with that. Then I found out you can calculate PI to a digit of your choice with the BBP Formula.

My problem is the BBP is in 16 base and i need this number in 10 base.
My current code looks like this:

double pi = 0;
for (int k = 0; k <=25; k++)
{
    pi = Math.Pow(16.0,-k) *
    ((4.0/(8*k+1.0))-
    (2.0/(8*k+4.0))-
    (1.0/(8*k+5.0))-
    (1.0/(8*k+6.0)));
    Console.WriteLine(pi.ToString());
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey(true);




该代码的输出如下:

5.06722085385879E-06
1.8789290093772E-07
7.76775121517736E-09
3.44793293050862E-10
1.6091877155537E-11
7.79570295400102E-13
3.88711525990975E-14
1.98322539359813E-15
1.03097121697889E-16
5.44347406057178E-18
2.91211179438419E-19
1.57549800977008E-20
8.60692632700397E-22
4.74204674455623E-23
2.63228669401317E-24
1.47090939027733E-25
8.2683300282764E-27
4.67271101635284E-28
2.65348590144992E-29
1.51345479607531E-30
8.66682856034781E-32
4.98130681533109E-33
2.87270191974158E-34
按任意键继续...


所以现在这是我被困的地方.
我到底是怎么把那些长的十进制数字变成"1415926535897932384626433"的.

再次感谢!




And the out put of this code is below:

5.06722085385879E-06
1.8789290093772E-07
7.76775121517736E-09
3.44793293050862E-10
1.6091877155537E-11
7.79570295400102E-13
3.88711525990975E-14
1.98322539359813E-15
1.03097121697889E-16
5.44347406057178E-18
2.91211179438419E-19
1.57549800977008E-20
8.60692632700397E-22
4.74204674455623E-23
2.63228669401317E-24
1.47090939027733E-25
8.2683300282764E-27
4.67271101635284E-28
2.65348590144992E-29
1.51345479607531E-30
8.66682856034781E-32
4.98130681533109E-33
2.87270191974158E-34
Press any key to continue...


So now here is the place i am stuck at.
How in the world do i get those long decimal numbers to turn into "1415926535897932384626433".

Thanks once again!

推荐答案

那些不是以16为基数的数字,而是以科学计数法显示的以10为基数(十进制)的数字.使用String.Format()方法以所需的格式显示.
Those are not base 16 numbers, they are base 10 (decimal) numbers displayed in scientific notation. Use the String.Format() method to display in the format that you require.


再次感谢texcodec,我使用了它,并且得到了正确的值,无需更改基数.

Hi again texcodec, i used this and I get correct values, no need to change the base.

3,13333333333333
0,00808913308913309
0,000164923924115101
5,06722085385879E-06
1,8789290093772E-07
7,76775121517736E-09
3,44793293050862E-10
1,6091877155537E-11
7,79570295400102E-13
3,88711525990975E-14
Press any key to continue...



或更好的pi + =以获得每次计算的合计值.



or better pi += to get the agregate value of every calculation.

3,13333333333333
3,14142246642247
3,14158739034658
3,14159245756744
3,14159264546034
3,14159265322809
3,14159265357288
3,14159265358897
3,14159265358975
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
3,14159265358979
Math.PI ->3,14159265358979
Press any key to continue...



我看到有关此方法的两件事:

1-. k不等于pi的第n个数字,我的意思是,当k = 1时,您可以获得pi的3个正确小数,其中k = 2-> 4,其中k = 3-> 6,k = 4→ 7,因此,如果您要求输入位数,则这不是要执行的位数.
2-.如果k> 9,则double值不足以显示这样的精度,因此您将获得与Math.PI一样好的性能.

我在另一篇文章中链接过的类给pi作为字符串,我想在每个bucle中您都会得到一个新数字,将其附加到现有的pi字符串上,因此bucle的数量( k )给您第n个数字.

因此,恭喜,看到代码后,公式就可以正常工作了.


关于



I see 2 thing about this method:

1-. k is no equal to nth digit of pi, I mean with k=1 you get 3 correct decimal of pi, with k=2 -> 4, with k=3 -> 6, k=4 -> 7, so if you ask for a number of digit this is not directly the number of for to execute.
2-.With k>9 the double value is not enough to show such precission, so you''ll get as better as Math.PI.

The class I linked you in the other post give pi as a string, and I thing that in every bucle you get a new digit, appending it to existing pi string, so the number of bucle (k) gives you the nth digit.

So seeing your code you got the formula working correctly, congratulations.


Regards


您可以使用Convert.ToInt32方法将以16为底的数字转换为以10为底的数字.它有一个重载,您可以在其中将输入作为字符串输入,并将基数作为数字.像这样:

You can convert base 16 number to base 10 using Convert.ToInt32 method. It has an overload where you can give the input as a string and base as a number. Like this:

int number = Convert.ToInt32("base 16 number", 16);



更新:误解了这个问题.这次可能知道了. :)

以下对我来说效果很好.



Update: Misunderstood the question. This time probably got it. :)

Following works fine for me.

double.Parse("exponential number as string here", System.Globalization.NumberStyles.Any);



如果仍然不适合您,则可能需要使用数字的字符串表示形式进行显示.



If it still doesn''t for you, then you may have to use string representation of the numbers for display.


这篇关于如何将以16为底的数字转换为以10为底的数字.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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