I am experimenting with a JavaScript library for working with big integers called BigInteger.js.
Specifically, I am trying to print a big integer. However, I am getting puzzling results. For instance, when I try this:
var x= bigInt("53542885039854749852");
document.write(x+"<br>");
...I am getting 53542885039854750000
. And when I try this:
var x= bigInt("104156103156113102156118165104101120101");
document.write(x+"<br>");
...the result is 1.041561031561131e+38
.
Can someone help me understand why I am not getting the results I expect?
I am experimenting with a JavaScript library for working with big integers called BigInteger.js.
Specifically, I am trying to print a big integer. However, I am getting puzzling results. For instance, when I try this:
var x= bigInt("53542885039854749852");
document.write(x+"<br>");
...I am getting 53542885039854750000
. And when I try this:
var x= bigInt("104156103156113102156118165104101120101");
document.write(x+"<br>");
...the result is 1.041561031561131e+38
.
Can someone help me understand why I am not getting the results I expect?
Share Improve this question edited Dec 29, 2018 at 21:22 DavidRR 19.5k27 gold badges111 silver badges197 bronze badges asked Jun 18, 2016 at 14:53 JORdJORd 1091 silver badge10 bronze badges 01 Answer
Reset to default 7You could use the toString()
method to get a string with all digits.
Note that arithmetical operators will trigger the
valueOf
function rather than thetoString
function. When converting a bigInteger to a string, you should use the toString method or the String function instead of adding the empty string.
bigInt("999999999999999999").toString() => "999999999999999999"
String(bigInt("999999999999999999")) => "999999999999999999"
bigInt("999999999999999999") + "" => 1000000000000000000
var x= bigInt("53542885039854749852");
document.write(x + "<br>"); // this calls valueOf
document.write(x.valueOf() + "<br>");
document.write(x.toString() + "<br>");
<script src="http://peterolson.github./BigInteger.js/BigInteger.min.js"></script>
To calculate something, you could use the methods of the class.
var y = bigInt("104156103156113102156118165104101120101");
document.write(y.plus(1).toString() + "<br>");
document.write(y.multiply(2).toString() + "<br>");
document.write(y.plus(y).toString() + "<br>");
<script src="http://peterolson.github./BigInteger.js/BigInteger.min.js"></script>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745303494a4621577.html
评论列表(0条)