Author

Topic: [ANN] Bitcoin currency wrapper for PHP (Read 858 times)

newbie
Activity: 37
Merit: 0
February 19, 2014, 11:53:07 AM
#3
any demo ?
Not yet. You can use usage examples as demo (just add
or echo separately).

var_dump(new BTC("1.1234")); should help too.

Documentation is needed and some changes will be made in the future. Tests are needed also. This is alpha.
legendary
Activity: 2394
Merit: 1216
The revolution will be digital
February 19, 2014, 11:31:38 AM
#2
any demo ?
newbie
Activity: 37
Merit: 0
February 19, 2014, 10:58:36 AM
#1
Bitcoin currency wrapper for PHP | version alpha

Bitcoin currency wrapper is collection of number wrappers with adjustable precision for PHP.

Github
https://github.com/misog/btc-currency-wrapper-php

Units
  • Bitcoin (BTC) = 1 BTC
  • Satoshi (sBTC) = 0.00000001 BTC
  • Centbitcoin (cBTC) = 0.01 BTC
  • Milibitcoin (mBTC) = 0.001 BTC
  • Microbitcoin (uBTC) = 0.000001 BTC
  • Atomicbitcoin (atomicBTC) = currently Satoshi

Arithmetic and logical operations
  • sub
  • mul
  • div
  • abs
  • less
  • greater
  • equals

Classes and structure
- Number
   - Currency extends Number
      - BTC extends Currency
         - cBTC extends BTC
         - mBTC extends BTC
         - uBTC extends BTC
         - sBTC extends BTC
         - atomicBTC extends BTC
      - EUR extends Currency

Usage examples
   
Code:
/* Basic operations */
$btc1 = new BTC("1");
$btc2 = new BTC("0.005");
$btc3 = $btc1->add($btc2);
echo $btc1->getValue(); // "1.00000000"
echo $btc2->getValue(); // "0.00500000"
echo $btc3->getValue(); // "1.00500000"

/* Operations with different units */
$btc = new BTC("2");
$s_btc = new sBTC("1"); // Satoshi
echo $btc->add($s_btc)->getValue(); // "2.00000001"
echo $s_btc->add($btc)->getValue(); // "200000001"
echo $s_btc->add($btc)->getBitcoinValue(); // "2.00000001"

/* Unit conversion */
$converted_btc = new BTC($s_btc);
echo $converted_btc->getValue(); // "0.00000001"
$converted_satoshi = new sBTC($btc);
echo $converted_satoshi->getValue(); // "200000000"

/* Precision */
$btc = new BTC("2");
$result = $btc->div(new Number('3'));
echo $result->getValue(); // "0.66666666" (truncated), not rounded "0.66666667" - better lost Satoshi than arise Satoshi
   
License
MIT - http://opensource.org/licenses/MIT

Donations
BTC: 1ByESbFGcfqcQU5fV2TNoHj7VdUAT6HsjJ
Jump to: