0

I have the string "9999999999999.99999" which I covert using Parse Float. But it changes the value to 10000000000000 , is there anyway I can have the get the number same without rounding off?

The input is string and out must be number not string. ex: ParseFloat("9999999999999.99999") = 9999999999999.99999 . So the output should not be string instead number

Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • 1
    Counter question - do you *need* this as a number? If you're not doing any mathematical operations, it shouldn't matter what the type of the data is. – VLAZ Jun 03 '22 at 06:27
  • Yes it has to number as its an input field which user types. so it has to be saved as it is @VLAZ – 007DevilsWorkshop Jun 03 '22 at 06:29
  • If you need arbitrary precision decimal numbers in JavaScript, you need to use an existing library or roll your own `BigDecimal` implementation. See also [this question](https://stackoverflow.com/questions/16742578/bigdecimal-in-javascript). – Robby Cornelissen Jun 03 '22 at 06:31
  • try `BigInt(your string here)` – I-vasilich-I Jun 03 '22 at 06:31
  • 1
    The user can type a number but *unless you do maths on it*, there is little need to save it as number. If you're only displaying it, then there is literally no difference between number and string. You can *validate* if it's a number but there is little need to *convert it*. – VLAZ Jun 03 '22 at 06:31
  • @I-vasilich-I `9999999999999.99999` is not an integer. – Robby Cornelissen Jun 03 '22 at 06:31
  • @I-vasilich-I the number in the question is not an integer. – VLAZ Jun 03 '22 at 06:31

2 Answers2

1

Well there is a bit limit to what numbers Javascript can handle (until we get BigInt64 or if you use a library like decimal.js). So since it cannot handle more decimals it just truncates at a point. If you would make a bigger number you would see less decimals. If this then leads to the number being "exacly" 9999999999999.99 in your case javascript will correctly show it as 9999999999999.99 and not 9999999999999.99999 since we only have Number and not float, decimal, int, etc.

parseFloat(9999999999999.99999);
// 9999999999999.99
parseFloat(9999999999999.9999);
// 9999999999999.99
parseFloat(9999999999999.999);
// 9999999999999.998
parseFloat(9999999999999.99);
// 9999999999999.99

Edit: actually it seem to round and not drop.

I hope that explains things.

Nbody
  • 1,168
  • 7
  • 33
0

You could have a look at decimal.js, this allows an arbitrary level of precision to be specified.

const x = new Decimal("9999999999999.99999");
console.log('x:', x.toString())

// Manipulate x
console.log('x * 2:', x.mul(2).toString())
console.log('x / 2:', x.div(2).toString())
<script src="https://cdnjs.cloudflare.com/ajax/libs/decimal.js/9.0.0/decimal.min.js" integrity="sha512-zPQm8HS4Phjo9pUbbk+HPH3rSWu5H03NFvBpPf6D9EU2xasj0ZxhYAc/lvv/HVDWMSE1Autj19i6nZOfiVQbFQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40
  • the output will be string is it ? @Terry – 007DevilsWorkshop Jun 03 '22 at 08:26
  • The .toString() call will output the decimal value as a string, however internally it is represented with digits array, exponent and sign. So any mathematical operations performed on it will give an accurate result. – Terry Lennox Jun 03 '22 at 08:36
  • You can use .toNumber() to convert to a JavaScript number, but risk losing precision. – Terry Lennox Jun 03 '22 at 08:39
  • Yeah but here I need to keep it as number without losing precision, I cant pass it as string as the logic expects number @Terry – 007DevilsWorkshop Jun 03 '22 at 08:53
  • The problem being you can't represent 9999999999999.99999 as a Number in JavaScript, it's a limitation of the fact they are stored as 64-bit doubles. So as soon as we convert to a Number precision will be lost. If the logic accepted a Decimal value then this could be made to work. – Terry Lennox Jun 03 '22 at 09:01