(function() {
//--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//JAVASCRIPT BITCOIN CONVERSION FUNCTIONS - v1.2
//by Izzukay Bell - 01abr13 - siliconVega.com - c0nt4ct4r[at]gmail[dot]com
//Nu in this version: Applied Ogig improvements and suggestions (Thanks to Ogig)
//Please, donations here: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u
//;)
//God shave the Queen!!
//--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
btcConv = {};
//CONFIG --------------------------------------------------------------
//PUT HERE the BTC current or historic value (in your currency units)
btcConv.val = 56.6667; //this is requested by * conversion functions
//REQUESTED FUNCTIONS -------------------------------------------------
//this is requested by ' conversion functions
//isFloat
//fm: http://www.forosdelweb.com/f13/como-saber-si-numero-decimal-entero-963718/#post4065830
btcConv.isFloat = function(n) {
return n % 1 != 0;
}
//this is requested by ' conversion functions
//dlZ [deleteZeroes] (delete float at only zeroes case)
btcConv.dlZ = function(n) {
var o = ( parseInt(n.toString().split('.')[1]) == 0) ? parseInt(n) : n;
return o;
}
//CONVERSION FUNCTIONS -------------------------------------------------
//From BTCs to currency units ----------------- * '
btcConv.btc2crr = function(btc) {
return btcConv.isFloat(btc*btcConv.val) ? btcConv.dlZ( (btc*btcConv.val).toFixed(4) ) : btc*btcConv.val;
}
//From BTCs to currency cents ----------------- * '
btcConv.btc2cnts = function(btc) {
return btcConv.isFloat(btc*btcConv.val*100) ? btcConv.dlZ( (btc*btcConv.val*100).toFixed(2) ) : btc*btcConv.val*100;
}
//From BTCs to satoshis -----------------------
btcConv.btc2sts = function(btc) {
return Math.floor(btc*100000000);
}
//From BTCs to mBTCs -------------------------- '
btcConv.btc2mbtc = function(btc) {
return btcConv.isFloat(btc*1000) ? btcConv.dlZ( (btc*1000).toFixed(5) ) : btc*1000;
}
//From BTCs to uBTCs -------------------------- '
btcConv.btc2ubtc = function(btc) {
return btcConv.isFloat(btc*1000000) ? btcConv.dlZ( (btc*1000000).toFixed(2) ) : btc*1000000;
}
//From currency units to BTCs ----------------- * '
btcConv.crr2btc = function(crr) {
return btcConv.isFloat(crr/btcConv.val) ? btcConv.dlZ( (crr/btcConv.val).toFixed(8) ) : crr/btcConv.val;
}
//From currency units to mBTCs ---------------- * '
btcConv.crr2mbtc = function(crr) {
return btcConv.isFloat( (crr/btcConv.val)*1000 ) ? btcConv.dlZ( ( (crr/btcConv.val)*1000 ).toFixed(5) ) : (crr/btcConv.val)*1000;
}
//From currency units to uBTCs ---------------- * '
btcConv.crr2ubtc = function(crr) {
return btcConv.isFloat( (crr/btcConv.val)*1000000 ) ? btcConv.dlZ( ( (crr/btcConv.val)*1000000 ).toFixed(2) ) : (crr/btcConv.val)*1000000;
}
//From currency units to satoshis ------------- *
btcConv.crr2sts = function(crr) {
return Math.floor( (crr/btcConv.val)*100000000 );
}
//From currency units to currency cents ------- '
btcConv.crr2cnts = function(crr) {
return btcConv.isFloat(crr*100) ? btcConv.dlZ( (crr*100).toFixed(2) ) : crr*100;
}
//From currency cents to BTCs ----------------- * '
btcConv.cnts2btc = function(cnts) {
return btcConv.isFloat( (cnts/100)/btcConv.val ) ? btcConv.dlZ( ( (cnts/100)/btcConv.val ).toFixed(8) ) : (cnts/100)/btcConv.val;
}
//From currency cents to mBTCs ---------------- * '
btcConv.cnts2mbtc = function(cnts) {
return btcConv.isFloat( (cnts/btcConv.val)*10 ) ? btcConv.dlZ( ( (cnts/btcConv.val)*10 ).toFixed(5) ) : (cnts/btcConv.val)*10;
}
//From currency cents to uBTCs ---------------- * '
btcConv.cnts2ubtc = function(cnts) {
return btcConv.isFloat( (cnts/btcConv.val)*10000 ) ? btcConv.dlZ( ( (cnts/btcConv.val)*10000 ).toFixed(2) ) : (cnts/btcConv.val)*10000;
}
//From currency cents to satoshis ------------- *
btcConv.cnts2sts = function(cnts) {
return Math.floor( (cnts/btcConv.val)*1000000 );
}
//From currency cents to currency units ------- '
btcConv.cnts2crr = function(cnts) {
return btcConv.isFloat(cnts/100) ? btcConv.dlZ( (cnts/100).toFixed(4) ) : cnts/100;
}
//From satoshis to BTCs ----------------------- '
btcConv.sts2btc = function(sts) {
return btcConv.isFloat(sts/100000000) ? btcConv.dlZ( (sts/100000000).toFixed(8) ) : sts/100000000;
}
//From satoshis to mBTCs ---------------------- '
btcConv.sts2mbtc = function(sts) {
return btcConv.isFloat(sts/100000) ? btcConv.dlZ( (sts/100000).toFixed(5) ) : sts/100000;
}
//From satoshis to uBTCs ---------------------- '
btcConv.sts2ubtc = function(sts) {
return btcConv.isFloat(sts/100) ? btcConv.dlZ( (sts/100).toFixed(2) ) : sts/100;
}
//From satoshis to currency units ------------- * '
btcConv.sts2crr = function(sts) {
return btcConv.isFloat(sts*btcConv.val/100000000) ? btcConv.dlZ( (sts*btcConv.val/100000000).toFixed(4) ) : sts*btcConv.val/100000000;
}
//From satoshis to currency cents ------------- * '
btcConv.sts2cnts = function(sts) {
return btcConv.isFloat(sts*btcConv.val/1000000) ? btcConv.dlZ( (sts*btcConv.val/1000000).toFixed(2) ) : sts*btcConv.val/1000000;
}
//From mBTCs to currency units ---------------- * '
btcConv.mbtc2crr = function(mbtc) {
return btcConv.isFloat( (mbtc/1000)*btcConv.val ) ? btcConv.dlZ( ( (mbtc/1000)*btcConv.val ).toFixed(4) ) : (mbtc/1000)*btcConv.val;
}
//From mBTCs to currency cents ---------------- * '
btcConv.mbtc2cnts = function(mbtc) {
return btcConv.isFloat( (mbtc/10)*btcConv.val ) ? btcConv.dlZ( ( (mbtc/10)*btcConv.val ).toFixed(2) ) : (mbtc/10)*btcConv.val;
}
//From mBTCs to satoshis ----------------------
btcConv.mbtc2sts = function(mbtc) {
return Math.floor(mbtc*100000);
}
//From mBTCs to BTCs -------------------------- '
btcConv.mbtc2btc = function(mbtc) {
return btcConv.isFloat(mbtc/1000) ? btcConv.dlZ( (mbtc/1000).toFixed(8) ) : mbtc/1000;
}
//From mBTCs to uBTCs ------------------------- '
btcConv.mbtc2ubtc = function(mbtc) {
return btcConv.isFloat(mbtc*1000) ? btcConv.dlZ( (mbtc*1000).toFixed(2) ) : mbtc*1000;
}
//From uBTCs to currency units ---------------- * '
btcConv.ubtc2crr = function(ubtc) {
return btcConv.isFloat( (ubtc/1000000)*btcConv.val ) ? btcConv.dlZ( ( (ubtc/1000000)*btcConv.val ).toFixed(4) ) : (ubtc/1000000)*btcConv.val;
}
//From uBTCs to currency cents ---------------- * '
btcConv.ubtc2cnts = function(ubtc) {
return btcConv.isFloat( (ubtc/10000)*btcConv.val ) ? btcConv.dlZ( ( (ubtc/10000)*btcConv.val ).toFixed(2) ) : (ubtc/10000)*btcConv.val;
}
//From uBTCs to satoshis ----------------------
btcConv.ubtc2sts = function(ubtc) {
return Math.floor(ubtc*100);
}
//From uBTCs to mBTCs ------------------------- '
btcConv.ubtc2mbtc = function(ubtc) {
return btcConv.isFloat(ubtc/1000) ? btcConv.dlZ( (ubtc/1000).toFixed(5) ) : ubtc/1000;
}
//From uBTCs to BTCs -------------------------- '
btcConv.ubtc2btc = function(ubtc) {
return btcConv.isFloat(ubtc/1000000) ? btcConv.dlZ( (ubtc/1000000).toFixed(8) ) : ubtc/1000000;
}
}());
//TEST FUNCTIONS -------------------------------------------------------
//Catch currency symbol
var crrSymb = function(crr) {
if(crr == 'EUR') return String.fromCharCode(8364);
if(crr == 'USD') return String.fromCharCode(36);
if(crr == 'GBP') return String.fromCharCode(163);
if(crr == 'JPY') return String.fromCharCode(165);
return crr;
}
//Test 1
var bfTest1 = function(crrId) {
var crrStr = crrSymb(crrId);
var _text = '>>>>>>> TEST 1: UNITARY VALUES <<<<<<<<br/>';
_text += '- 1 BTC ---> ' + btcConv.btc2crr(1) + ' ' + crrStr + '<br/>';
_text += '- 1 BTC ---> ' + btcConv.btc2cnts(1) + ' c' + crrStr + '<br/>';
_text += '- 1 BTC ---> ' + btcConv.btc2sts(1) + ' sts<br/>';
_text += '- 1 BTC ---> ' + btcConv.btc2mbtc(1) + ' mBTC<br/>';
_text += '- 1 BTC ---> ' + btcConv.btc2ubtc(1) + ' uBTC<br/>';
_text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2btc(1) + ' BTC<br/>';
_text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2mbtc(1) + ' mBTC<br/>';
_text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2ubtc(1) + ' uBTC<br/>';
_text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2sts(1) + ' sts<br/>';
_text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2cnts(1) + ' c' + crrStr + '<br/>';
_text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2btc(1) + ' BTC<br/>';
_text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2mbtc(1) + ' mBTC<br/>';
_text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2ubtc(1) + ' uBTC<br/>';
_text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2sts(1) + 'sts<br/>';
_text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2crr(1) + crrStr + '<br/>';
_text += '- 1 sts ------> ' + btcConv.sts2btc(1) + ' BTC<br/>';
_text += '- 1 sts ------> ' + btcConv.sts2mbtc(1) + ' mBTC<br/>';
_text += '- 1 sts ------> ' + btcConv.sts2ubtc(1) + ' uBTC<br/>';
_text += '- 1 sts ------> ' + btcConv.sts2crr(1) + ' ' + crrStr + '<br/>';
_text += '- 1 sts ------> ' + btcConv.sts2cnts(1) + ' c' + crrStr + '<br/>';
_text += '- 1 mBTC --> ' + btcConv.mbtc2btc(1) + ' BTC<br/>';
_text += '- 1 mBTC --> ' + btcConv.mbtc2crr(1) + ' ' + crrStr + '<br/>';
_text += '- 1 mBTC --> ' + btcConv.mbtc2cnts(1) + ' c' + crrStr + '<br/>';
_text += '- 1 mBTC --> ' + btcConv.mbtc2sts(1) + ' sts<br/>';
_text += '- 1 mBTC --> ' + btcConv.mbtc2ubtc(1) + ' uBTC<br/>';
_text += '- 1 uBTC ---> ' + btcConv.ubtc2btc(1) + ' BTC<br/>';
_text += '- 1 uBTC ---> ' + btcConv.ubtc2crr(1) + ' ' + crrStr + '<br/>';
_text += '- 1 uBTC ---> ' + btcConv.ubtc2cnts(1) + ' c' + crrStr + '<br/>';
_text += '- 1 uBTC ---> ' + btcConv.ubtc2sts(1) + ' sts<br/>';
_text += '- 1 uBTC ---> ' + btcConv.ubtc2mbtc(1) + ' mBTC<br/><br/>';
return _text;
}
//Test 2
var bfTest2 = function(crrId) {
var crrStr = crrSymb(crrId);
var _text = '>>>>>>> TEST 2: EXAMPLE VALUES <<<<<<<<br/>';
_text += '- 13 BTC --------------> ' + btcConv.btc2crr(13) + ' ' + crrStr + '<br/>';
_text += '- 13 BTC --------------> ' + btcConv.btc2cnts(13) + ' c' + crrStr + '<br/>';
_text += '- 13 BTC --------------> ' + btcConv.btc2sts(13) + ' sts<br/>';
_text += '- 13 BTC --------------> ' + btcConv.btc2mbtc(13) + ' mBTC<br/>';
_text += '- 13 BTC --------------> ' + btcConv.btc2ubtc(13) + ' uBTC<br/>';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2btc(736.6671) + ' BTC<br/>';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2mbtc(736.6671) + ' mBTC<br/>';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2ubtc(736.6671) + ' uBTC<br/>';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2sts(736.6671) + ' sts<br/>';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2cnts(736.6671) + ' c' + crrStr + '<br/>';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2btc(73666.71) + ' BTC<br/>';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2mbtc(73666.71) + ' mBTC<br/>';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2ubtc(73666.71) + ' uBTC<br/>';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2sts(73666.71) + 'sts<br/>';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2crr(73666.71) + crrStr + '<br/>';
_text += '- 1300000000 sts ------> ' + btcConv.sts2btc(1300000000) + ' BTC<br/>';
_text += '- 1300000000 sts ------> ' + btcConv.sts2mbtc(1300000000) + ' mBTC<br/>';
_text += '- 1300000000 sts ------> ' + btcConv.sts2ubtc(1300000000) + ' uBTC<br/>';
_text += '- 1300000000 sts ------> ' + btcConv.sts2crr(1300000000) + ' ' + crrStr + '<br/>';
_text += '- 1300000000 sts ------> ' + btcConv.sts2cnts(1300000000) + ' c' + crrStr + '<br/>';
_text += '- 13000 mBTC --------> ' + btcConv.mbtc2btc(13000) + ' BTC<br/>';
_text += '- 13000 mBTC --------> ' + btcConv.mbtc2crr(13000) + ' ' + crrStr + '<br/>';
_text += '- 13000 mBTC --------> ' + btcConv.mbtc2cnts(13000) + ' c' + crrStr + '<br/>';
_text += '- 13000 mBTC --------> ' + btcConv.mbtc2sts(13000) + ' sts<br/>';
_text += '- 13000 mBTC --------> ' + btcConv.mbtc2ubtc(13000) + ' uBTC<br/>';
_text += '- 13000000 uBTC -----> ' + btcConv.ubtc2btc(13000000) + ' BTC<br/>';
_text += '- 13000000 uBTC -----> ' + btcConv.ubtc2crr(13000000) + ' ' + crrStr + '<br/>';
_text += '- 13000000 uBTC -----> ' + btcConv.ubtc2cnts(13000000) + ' c' + crrStr + '<br/>';
_text += '- 13000000 uBTC -----> ' + btcConv.ubtc2sts(13000000) + ' sts<br/>';
_text += '- 13000000 uBTC -----> ' + btcConv.ubtc2mbtc(13000000) + ' mBTC<br/><br/>';
return _text;
}
//Launch
document.write( bfTest1('EUR') + bfTest2('EUR') + '<br/>Useful? Please donate: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u<br/>THNX!! ;)' );