Decimal places in Javascript

Asked

Viewed 377 times

2

What type of variable can I use to generate as many decimals as possible in Javascript?

Ex.: 1.98719751097319284791284710237182479182471209381290481284

If possible, of course =)

2 answers

3


Javascript only has a numeric type, so it should use it. And of course, it has a maximum and it is this maximum that you can use.

It is good to know that JS uses a numerical type based on binary floating point and this means that there will be no numerical accuracy, if you need this does not give, but for the above it does not seem to be the case. The maximum is the Number.MAX_VALUE.

console.log(Number.MAX_VALUE);

I put in the Github for future reference.

If this does not solve you will have to do or take a library that creates a type that meets your need.

  • JS does not need to set a type for the variable just use var before, but inside she makes some kind of test to define the type for this variable?

  • This would be as high as possible or as many decimal places as possible?

  • @bfavaretto as it is in scientific notation whatever makes ;)

  • I tried to do the math to see if it beat, but I gave up in the middle rs. But I’ve been reading that the precision is around 15 digits, independent of the decimal point position. So it shouldn’t matter too much how many houses you can represent after that. I think. :)

  • @Guilhermecostamilam note that I have not spoken in variable, I have spoken in value, in dynamic typing languages the variables have no type, but the values always have. In these languages there is a tag together with the value then the variable has a type according to its value and this is tested internally to know what to do, including give error.

  • @bfavaretto is actually a little more complicated, as can be seen has more than 300 houses. This is not to say that all numbers can be represented, but it is possible to have that many houses. There are the most significant, in the case the representation showed 16 and "swallowed" 308.

Show 1 more comment

1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.