moneyphp / money

PHP implementation of Fowler's Money pattern.
http://moneyphp.org
MIT License
4.62k stars 440 forks source link

Cryptocurrency Advice #755

Closed booni3 closed 10 months ago

booni3 commented 1 year ago

I have been using this package for years and love it. I have always stored my money as integers or big integers, which has worked great.

I am on a new project that requires flexibility on both sides of the decimal point. A BIGINT will only give us 8-10 on either side, which is insufficient.

MySQL DECIMAL(65,32) is far more flexible but rarely mentioned as a good idea for storing currencies. I understand there is some performance degradation vs. integers, but is there anything else I should know about for use generally and within this package?

Ideally, I need to perform SUM aggregations on the database so strings are out. The other option would be using 2 BIGINT columns for either side of the decimal, but this adds too much complexity.

ismaail commented 1 year ago

You can create your own Currency (example: BitcoinCurrencies)

your can set how many decimals this currency have with the const SUBUNIT

use Money\Currencies;

final class MyCurrency implements Currencies
{
    private const SUBUNIT = 5;
}
frederikbosch commented 10 months ago

@booni3 This place is not for questions like these. Having said that, maybe suggestion 2 might work.

  1. Two columns if the sum operations are required to perform.
  2. A single string column, and in your sum operation you select the integer and fractional part using string operations, then cast it to (big)int and then apply SUM, e.g. SELECT CAST(LEFT(money, POSITION(',' in money)) AS int) AS money_integer, CAST(RIGHT(money, POSITION(',' in money)) AS int) AS money_fractional