Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Draft saved
Draft discarded
Cancel
9
  • May be duplicate: stackoverflow.com/questions/6876666/… Commented Jan 9, 2025 at 13:25
  • 1
    Depending on a cornucopia of factors, that last 5 may not actually be stored as part of the float's value in the first place. In a nutshell, with floats you can basically never depend on the exact value you'll get for any operation. All that's guaranteed is that you'll get a value close enough to your value, that it'll only occupy a specific memory size, and that operations on it will be pretty fast. For anything else, you need to use a different type. — What exactly is your use case? Commented Jan 9, 2025 at 13:30
  • 1
    You know that floating point numbers are of limited precision, roughly 14 to 16 digits, yet you struggle with it. If you write the original number as a string nothing will be lost, is that a possibility for you? There is also something like BCMath, and others. The problem is that we don't know why you're having a problem with this. As deceze says: What's your use case? Commented Jan 9, 2025 at 13:38
  • @deceze I want to be able to show it as is in the frontend (so I assume I have to have always keep it as string without type casting), and a user may send a float which must be kept as is (so I assume again this must be always a string then), and I want to be able to compare certain values. I could work for that use case to both cast them the same way, as long as the same "error" is applied, it is fine. I was wondering about BCMath, yet that may be overkill. Commented Jan 9, 2025 at 14:05
  • 2
    Keeping user input as strings might be a good idea. After all, users basically do input strings. However, as soon as you want to use these strings as pure floats you run into the same problem. Notice how BCMath simply uses strings to avoid this problem. It might not be overkill. Commented Jan 9, 2025 at 14:10

lang-php

AltStyle によって変換されたページ (->オリジナル) /