It is a little-regarded truth of computer programming that when you're dealing with money you should avoid the usual IEEE floating-point math like the plague. Converting satoshis to bitcoins issue using PHP. It is generally accepted that the protocol can be changed to support smaller units, if required. Is that really a serious risk? Satoshi to BTC Mathematical formula: Bitcoin Forum January 31, , Of course this depends on the precision required though.
Seems like parseFloat is good: I know this is kind of something you can google yourself or probably have , but here's a couple SO links with some more info. Of course this depends on the precision required though. Hero Member Offline Activity: Because storing them as floating point numbers is error-prone. For example, in your interactive PHP shell you can do something like this to find out where your math begins to break:.
The take-away here is that overall precision of floating point numbers is limited. The bigger your number, the fewer meaningful decimal places it can have. You keep your data as strings, and BCMath will let you perform arbitrary-precision computations. It isn't as fast as floating point math, but it isn't exactly slow either.
If you want to trim the zeros from the end of the output, you can use the trim function:. You should use bcmath , because PHP's native calculations won't fit your needs. Since you have not picked an answer, can I suggest a function?
Bitcoin Stack Exchange is a question and answer site for Bitcoin crypto-currency enthusiasts. Join them; it only takes a minute: Here's how it works: Anybody can ask a question Anybody can answer The best answers are voted up and rise to the top. Converting satoshis to bitcoins issue using PHP. Ford 63 1 6. Please note that one should never use floating point in money calculations, as it may lose accuracy. This question appears to be off-topic because it is about php programming.