2

Problem I face: How to work with Money in PHP if currencies are Bitoins, Litecoins, Etherum and others alt-coins?

This problem in case of fiat money is solved mostly by representing money as int in the smallest unit (cents, etc...).

But as crypto-coins are represented mostly as number like 0.0235678 and the smallest unit is not so clear.

Yes, for Bitcoins there are Satoshi, Etherum has Tether and other crypto coins are similar mechanics. But as those currencies are designed to be deflationary, there is theoretical chance that the smaller units will be needed in the future.

I would like to solve this too.

Exact numbers are very often represented as string. And therefore using of http://php.net/manual/en/book.bc.php came up to my mind.

  • Does anyone have any other alternative?
  • Or there is some implemention of Money object in PHP (I didn't find any).

Now, I see only option to implement my own Money object internally using bcmath.

Thanks for suggestions.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • Ordinarily, you can use a `CURRENCY` data-type (in an SQL database) to represent "currency-amounts of any sort." Although concrete implementations vary from product to product, all of them are specifically designed to consider the problem of "fractional pennies." For example, the MS-Access database stores currency amounts as long-integers that are multiplied by 1000. This gives you four *fixed-point* digits to the right of the decimal. Integer math is functionally equivalent to BCD. Although "the currency in question" is exotic, the problem itself is old-hat. – Mike Robinson Jul 08 '16 at 19:31
  • P.S. You should also bear in mind that "PHP is rather mathematically-stupid." It really only knows about floating-point. – Mike Robinson Jul 08 '16 at 19:38
  • Mike, thanks for replay. But I am trying to solve how to represent bitcoin money in PHP not SQL. Also, I don't want "fixed" number of digits on the right, I need possibility to have infinite of them if need and also avoid precision problems. `bcmath` in PHP works internally with strings and can provide that. But I am not sure if there is other option or if I didnt overlook something. –  Jul 08 '16 at 21:25
  • Very definitely, I would move the data to an immediate representation of "String." (Definitely use a library like `bcmath` or what-have-you.) What you want to be sure to stay *far away* from ... is "PHP's floating-point." Although I am certainly no expert in this subject-area, my *intuitive* notion is that you're on a good track. Move the coin-tokens immediately to a *string-based* representation, then use a BCD-math llbrary to handle them from there. Seems to me ("albeit, from a bit of a distance") that this is likely to be a good wager. – Mike Robinson Jul 09 '16 at 02:30

0 Answers0