Bitcoin Forum
June 14, 2024, 04:43:43 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Storing Bitcoin precision numbers with PHP.  (Read 744 times)
Remember remember the 5th of November (OP)
Legendary
*
Offline Offline

Activity: 1862
Merit: 1011

Reverse engineer from time to time


View Profile
April 26, 2013, 05:38:01 AM
 #1

I am writing an exchange in PHP as an exercise to see how they work, just an exercise nothing for real-world application. I haven't done PHP in a very long time I have become so rusty many basics are still hard to remember.

I want to store a number of say 87.12471115 bitcoins as a 64bit integer in MySQL(i), where my field is of type BIGINT. I want to store it without any precision loss, or need for rounding so that no matter what number is entered it's ALWAYS printed exactly the same way it was entered, not even a single digit being higher or lower.

Like entering 87.12471115 and then fetching the value as 87.12471116 or 87.12471114 or even worse 87.13 <-- BAD.

So how do I process the number? In Bitcoin it's multiplied by 100,000,000(64bit integer) which a hundred million.

Code:
<?php

$amount 
$_POST['amount']; // the user has entered 87.12471115 which gets interpreted either as a string OR a double.

// what should be done here?

mysqli_query(....); // insert the amount here

As you can see it either gets treated as a double or a string. How would the process of double to integer happen?

BTC:1AiCRMxgf1ptVQwx6hDuKMu4f7F27QmJC2
lucif
Sr. Member
****
Offline Offline

Activity: 462
Merit: 250


Clown prophet


View Profile
April 26, 2013, 05:46:50 AM
 #2

I think the following

Code:
$amount_int64=bcmul($amount,'100000000',0);

EDIT: keep in mind, BCmath keeps large numbers as string variables. So your $amount_int64 will be a string for php.
Bitsky
Hero Member
*****
Offline Offline

Activity: 576
Merit: 514


View Profile
April 26, 2013, 03:26:02 PM
 #3

Or store them as Satoshi. Int on 64bit system should be large enough to handle them.

Bounty: Earn up to 68.7 BTC
Like my post? Feel free to drop a tip to 1BitskyZbfR4irjyXDaGAM2wYKQknwX36Y
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!