0

I am developing a site with javascript, which does the following, the visitor will enter the unit quantity, the unit to compare and the unit of comparison (Ex: 12 centimeters for millimeter) and it will show how much the quantity of the unit to compare is equivalent to the unit of comparison, the only problem is that if I put a unit to compare too small and a unit to compare too large, it will happen that the result will be wrong (Ex: 17 decimeters for kilometers will give 0, 0170000000000001) so I asked a person and they replied that I should use scientific notation, but I have no idea how to do it in javascript, could anyone help me?

  • Could this be a good track ? https://stackoverflow.com/questions/11124451/how-can-i-convert-numbers-into-scientific-notation – Peterrabbit Apr 10 '22 at 17:50
  • 1
    Does this answer your question? [How can I convert numbers into scientific notation?](https://stackoverflow.com/questions/11124451/how-can-i-convert-numbers-into-scientific-notation) – Justinas Apr 10 '22 at 17:51
  • Google for a "javascript big number library", there are a bunch of choices so you should see which one will work best for you. Normal `Number` objects in JavaScript only have so much precision. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number – Billy Hudson Apr 10 '22 at 17:53
  • https://stackoverflow.com/questions/3072307/what-is-the-standard-solution-in-javascript-for-handling-big-numbers-bignum – Billy Hudson Apr 10 '22 at 17:56
  • While you can have JS give you the exponential representation of a `Number` as a string, you can't use it in calculations and you won't gain any precision as it is still represents the internal `Number` type. – Billy Hudson Apr 10 '22 at 18:09

1 Answers1

0

Do you mean something like this?

number.toExponential(x)

x can go from 0 to 20. It's the number of digits after the decimal point.

TwoZed3
  • 64
  • 3