Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: glorsh66 on July 20, 2017, 07:14:52 PM



Title: The best way to store and use currencies (PHP+MYSQL)
Post by: glorsh66 on July 20, 2017, 07:14:52 PM
I am developing a non profit project that uses a lot of forks (and bitcoin of course) - but's i can't decide what datatype use to store prices.
There are several ways to do this, but i have chosen two of them - to use BIGINTEGER, and store it as a STRING

1) - BIGINTEGER (in this case i keep pennie e.g. Satoshi) it's better to use in calculations - but i can't wrap my head around - how to properly take user input? What datatype should i use to use math operations in PHP, and what additional data i need to keep to convert on currency to another (i mean there are possibility that some forks can use more digits after point.)
2) STRING - just impossible to make any computations, before proper conversion. But it's much simper to keep - i mean you just can store the raw user's input   

Can somebody share some ideas with me about this matter?


Title: Re: The best way to store and use currencies (PHP+MYSQL)
Post by: jackg on July 20, 2017, 07:35:55 PM
I am developing a non profit project that uses a lot of forks (and bitcoin of course) - but's i can't decide what datatype use to store prices.
There are several ways to do this, but i have chosen two of them - to use BIGINTEGER, and store it as a STRING

1) - BIGINTEGER (in this case i keep pennie e.g. Satoshi) it's better to use in calculations - but i can't wrap my head around - how to properly take user input? What datatype should i use to use math operations in PHP, and what additional data i need to keep to convert on currency to another (i mean there are possibility that some forks can use more digits after point.)
2) STRING - just impossible to make any computations, before proper conversion. But it's much simper to keep - i mean you just can store the raw user's input   

Can somebody share some ideas with me about this matter?

An inneficient way of doing this is by you just using strings for everything. There is only addition and subtraction that is required in this I presume, so you can easily just encorporate math in that way.
Alternatively, you could try to use "Double" variable types or "Float" if php doesn't provide "double"s (doubles is with a desimal but is 64 bit floating point instead of 32 bit.
I assum BIGINTEGER represents INT64, but you could as well just use regular integers as they should be 232 and you probably don't need much higher than that.


Title: Re: The best way to store and use currencies (PHP+MYSQL)
Post by: Bitsky on July 21, 2017, 07:30:14 AM
I'm using DECIMAL (https://dev.mysql.com/doc/refman/5.7/en/precision-math-decimal-characteristics.html) for storing values, and so far that worked fine with calculations in SQL.
If you want to do math in PHP, you should look at BCMath (http://php.net/manual/en/book.bc.php).