-1

Here's my problem:

The user should be able to enter a price which might be a floating point number (0.59, 1.25 etc). I want to store the price as an integer value. My original solution is Int((Float(stringPrice) ?? 0) * 100) but there's an obvious problem with floating point numbers. For example if stringPrice is "0.59" the resulting integer appears to be 58.

What is the best way to handle such a converting?

UPD: This question has been marked as a duplicate and it is in some way but I'll leave it here for one reason: if one doesn't know that this problem can be reduced to the rounding one they will find this question (also as a proxy to the reduced question) useful

ramzesenok
  • 5,469
  • 4
  • 30
  • 41

1 Answers1

2

What I found useful till now is to use rounding:

Int(((Double(stringPrice) ?? 0) * 100).rounded())

This solves the problem but I'm wondering if there's a better (more elegant) way of doing it.


Short explanation why rounding works for those who wonder:

When working with floating point numbers the computer has its own ways of handling them in binary system (https://www.youtube.com/watch?v=PZRI1IfStY0). The thing is - the number you see and the number computer sees might differ. For example you might see 0.3 but the actual representation in binary might mean something like 0.30000000000000004 (try adding 0.1 + 0.2 in JavaScript in your browser console, you'll see it yourself).

So when you use rounding you essentially cut off everything beyond the decimal point. And the number becomes "clear" so to speak.

In this case we have a string "0.59". Converting it to Double first results in 0.59. Multiplying by 10 results in 59,0. Even if the actual representation involves some 1/10000000 in the end (e.g. 59,0000001) the rounding will cut it off. So after .rounded() the number will be the very same for us, humans, but might be much cleaner for the computer. Then casting it to integer results in a predictable 59.

ramzesenok
  • 5,469
  • 4
  • 30
  • 41