如何在javascript中显示超过20个小数点? [英] How to display more than 20 decimal points in javascript?

查看:106
本文介绍了如何在javascript中显示超过20个小数点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个显示pi数字的网页.我可以使用toFixed方法使pi变量显示pi的20位数字,但我想知道是否可以显示更多数字.小数位数是否受变量的内存大小限制?有没有办法显示20位以上的数字?

I am making a webpage that displays the digits of pi. I was able to get my pi variable to display 20 digits of pi using the toFixed method but I am wondering if I can display more. Is the number of decimal places limited by the memory size of my variable? Is there a way to display more than 20 digits?

我应该澄清一下,我正在使用Chudnovsky Algorithmn计算pi的值.我假设由于var变量的内存限制,它最多只能显示一定数量的数字,最多只能显示20个数字? 来源

I should have clarified that I was using the Chudnovsky Algorithmn to calculate the value of pi. I assuming that because of the memory limitations of the var variable that it can only display up to a certain amount of digits which is limited to 20? Source

推荐答案

更多信息,请参见以下内容强制转换为浮动:

The following is cast to float:

var x =  3.14159265358979323846;
print(x.toFixed(20));

但是,您可以打印字符串或使用 mathjs :

However, you can either print a string or to use mathjs:

var one = math.bignumber(1);
var pi = math.bignumber('3.14159265358979323846');
var ans = math.multiply(one, pi);
print(ans.toString());

会给你"3.14159265358979323846"

这篇关于如何在javascript中显示超过20个小数点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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