//--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//JAVASCRIPT BITCOIN CONVERSION FUNCTIONS - v1.1
//by Izzukay Bell - 30mar13 - siliconVega.com - c0nt4ct4r[at]gmail[dot]com
//Nu in this version: Added support for mBTC and uBTC.
//Nu functions: btc2mbtc, btc2ubtc, crr2mbtc, crr2ubtc, cnts2mbtc, cnts2ubtc, sts2mbtc,
// sts2ubtc, mbtc2crr, mbtc2cnts, mbtc2sts, mbtc2btc, mbtc2ubtc, ubtc2crr,
// ubtc2cnts, ubtc2sts, ubtc2mbtc, ubtc2btc
//Please, donations here: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u
//;)
//God shave the Queen!!
//--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//CONFIG --------------------------------------------------------------
//PUT HERE the BTC current or historic value (in your currency units)
var val = 56.6667; //this is requested by * conversion functions
//REQUESTED FUNCTIONS -------------------------------------------------
//this is requested by " conversion functions
//tfxp [toFixedPlus] (scientific notation off)
//fm: http://es.softuses.com/208551
function tfxp(x) {
if (Math.abs(x) < 1.0) {
var e = parseInt(x.toString().split('e-')[1]);
if (e) {
x *= Math.pow(10,e-1);
x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
}
} else {
var e = parseInt(x.toString().split('+')[1]);
if (e > 20) {
e -= 20;
x /= Math.pow(10,e);
x += (new Array(e+1)).join('0');
}
}
return x;
}
//this is requested by ' conversion functions
//isFloat
//fm: http://www.forosdelweb.com/f13/como-saber-si-numero-decimal-entero-963718/#post4065830
function isFloat(n) {
return n % 1 != 0;
}
//this is requested by ' conversion functions
//dlZ [deleteZeroes] (delete float at only zeroes case)
var dlZ = function(n) {
var o = ( parseInt(n.toString().split('.')[1]) == 0) ? parseInt(n) : n;
return o;
}
//CONVERSION FUNCTIONS -------------------------------------------------
//From BTCs to currency units ----------------- * " '
var btc2crr = function(btc) {
return isFloat(btc*val) ? dlZ( tfxp( (btc*val).toFixed(4) ) ) : btc*val;
}
//From BTCs to currency cents ----------------- * " '
var btc2cnts = function(btc) {
return isFloat(btc*val*100) ? dlZ( tfxp( (btc*val*100).toFixed(2) ) ) : btc*val*100;
}
//From BTCs to satoshis -----------------------
var btc2sts = function(btc) {
return Math.floor(btc*100000000);
}
//From BTCs to mBTCs -------------------------- " '
var btc2mbtc = function(btc) {
return isFloat(btc*1000) ? dlZ( tfxp( (btc*1000).toFixed(5) ) ) : btc*1000;
}
//From BTCs to uBTCs -------------------------- " '
var btc2ubtc = function(btc) {
return isFloat(btc*1000000) ? dlZ( tfxp( (btc*1000000).toFixed(2) ) ) : btc*1000000;
}
//From currency units to BTCs ----------------- * " '
var crr2btc = function(crr) {
return isFloat(crr/val) ? dlZ( tfxp( (crr/val).toFixed(8) ) ) : crr/val;
}
//From currency units to mBTCs ---------------- * " '
var crr2mbtc = function(crr) {
return isFloat( (crr/val)*1000 ) ? dlZ( tfxp( ( (crr/val)*1000 ).toFixed(5) ) ) : (crr/val)*1000;
}
//From currency units to uBTCs ---------------- * " '
var crr2ubtc = function(crr) {
return isFloat( (crr/val)*1000000 ) ? dlZ( tfxp( ( (crr/val)*1000000 ).toFixed(2) ) ) : (crr/val)*1000000;
}
//From currency units to satoshis ------------- *
var crr2sts = function(crr) {
return Math.floor( (crr/val)*100000000 );
}
//From currency units to currency cents ------- " '
var crr2cnts = function(crr) {
return isFloat(crr*100) ? dlZ( tfxp( (crr*100).toFixed(2) ) ) : crr*100;
}
//From currency cents to BTCs ----------------- * " '
var cnts2btc = function(cnts) {
return isFloat( (cnts/100)/val ) ? dlZ( tfxp( ( (cnts/100)/val ).toFixed(8) ) ) : (cnts/100)/val;
}
//From currency cents to mBTCs ---------------- * " '
var cnts2mbtc = function(cnts) {
return isFloat( (cnts/val)*10 ) ? dlZ( tfxp( ( (cnts/val)*10 ).toFixed(5) ) ) : (cnts/val)*10;
}
//From currency cents to uBTCs ---------------- * " '
var cnts2ubtc = function(cnts) {
return isFloat( (cnts/val)*10000 ) ? dlZ( tfxp( ( (cnts/val)*10000 ).toFixed(2) ) ) : (cnts/val)*10000;
}
//From currency cents to satoshis ------------- *
var cnts2sts = function(cnts) {
return Math.floor( (cnts/val)*1000000 );
}
//From currency cents to currency units ------- " '
var cnts2crr = function(cnts) {
return isFloat(cnts/100) ? dlZ( tfxp( (cnts/100).toFixed(4) ) ) : cnts/100;
}
//From satoshis to BTCs ----------------------- " '
var sts2btc = function(sts) {
return isFloat(sts/100000000) ? dlZ( tfxp( (sts/100000000).toFixed(8) ) ) : sts/100000000;
}
//From satoshis to mBTCs ---------------------- " '
var sts2mbtc = function(sts) {
return isFloat(sts/100000) ? dlZ( tfxp( (sts/100000).toFixed(5) ) ) : sts/100000;
}
//From satoshis to uBTCs ---------------------- " '
var sts2ubtc = function(sts) {
return isFloat(sts/100) ? dlZ( tfxp( (sts/100).toFixed(2) ) ) : sts/100;
}
//From satoshis to currency units ------------- * " '
var sts2crr = function(sts) {
return isFloat(sts*val/100000000) ? dlZ( tfxp( (sts*val/100000000).toFixed(4) ) ) : sts*val/100000000;
}
//From satoshis to currency cents ------------- * " '
var sts2cnts = function(sts) {
return isFloat(sts*val/1000000) ? dlZ( tfxp( (sts*val/1000000).toFixed(2) ) ) : sts*val/1000000;
}
//From mBTCs to currency units ---------------- * " '
var mbtc2crr = function(mbtc) {
return isFloat( (mbtc/1000)*val ) ? dlZ( tfxp( ( (mbtc/1000)*val ).toFixed(4) ) ) : (mbtc/1000)*val;
}
//From mBTCs to currency cents ---------------- * " '
var mbtc2cnts = function(mbtc) {
return isFloat( (mbtc/10)*val ) ? dlZ( tfxp( ( (mbtc/10)*val ).toFixed(2) ) ) : (mbtc/10)*val;
}
//From mBTCs to satoshis ----------------------
var mbtc2sts = function(mbtc) {
return Math.floor(mbtc*100000);
}
//From mBTCs to BTCs -------------------------- " '
var mbtc2btc = function(mbtc) {
return isFloat(mbtc/1000) ? dlZ( tfxp( (mbtc/1000).toFixed(8) ) ) : mbtc/1000;
}
//From mBTCs to uBTCs ------------------------- " '
var mbtc2ubtc = function(mbtc) {
return isFloat(mbtc*1000) ? dlZ( tfxp( (mbtc*1000).toFixed(2) ) ) : mbtc*1000;
}
//From uBTCs to currency units ---------------- * " '
var ubtc2crr = function(ubtc) {
return isFloat( (ubtc/1000000)*val ) ? dlZ( tfxp( ( (ubtc/1000000)*val ).toFixed(4) ) ) : (ubtc/1000000)*val;
}
//From uBTCs to currency cents ---------------- * " '
var ubtc2cnts = function(ubtc) {
return isFloat( (ubtc/10000)*val ) ? dlZ( tfxp( ( (ubtc/10000)*val ).toFixed(2) ) ) : (ubtc/10000)*val;
}
//From uBTCs to satoshis ----------------------
var ubtc2sts = function(ubtc) {
return Math.floor(ubtc*100);
}
//From uBTCs to mBTCs ------------------------- " '
var ubtc2mbtc = function(ubtc) {
return isFloat(ubtc/1000) ? dlZ( tfxp( (ubtc/1000).toFixed(5) ) ) : ubtc/1000;
}
//From uBTCs to BTCs -------------------------- " '
var ubtc2btc = function(ubtc) {
return isFloat(ubtc/1000000) ? dlZ( tfxp( (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 <<<<<<<
';
_text += '- 1 BTC ---> ' + btc2crr(1) + ' ' + crrStr + '
';
_text += '- 1 BTC ---> ' + btc2cnts(1) + ' c' + crrStr + '
';
_text += '- 1 BTC ---> ' + btc2sts(1) + ' sts
';
_text += '- 1 BTC ---> ' + btc2mbtc(1) + ' mBTC
';
_text += '- 1 BTC ---> ' + btc2ubtc(1) + ' uBTC
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2btc(1) + ' BTC
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2mbtc(1) + ' mBTC
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2ubtc(1) + ' uBTC
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2sts(1) + ' sts
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2cnts(1) + ' c' + crrStr + '
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2btc(1) + ' BTC
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2mbtc(1) + ' mBTC
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2ubtc(1) + ' uBTC
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2sts(1) + 'sts
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2crr(1) + crrStr + '
';
_text += '- 1 sts ------> ' + sts2btc(1) + ' BTC
';
_text += '- 1 sts ------> ' + sts2mbtc(1) + ' mBTC
';
_text += '- 1 sts ------> ' + sts2ubtc(1) + ' uBTC
';
_text += '- 1 sts ------> ' + sts2crr(1) + ' ' + crrStr + '
';
_text += '- 1 sts ------> ' + sts2cnts(1) + ' c' + crrStr + '
';
_text += '- 1 mBTC --> ' + mbtc2btc(1) + ' BTC
';
_text += '- 1 mBTC --> ' + mbtc2crr(1) + ' ' + crrStr + '
';
_text += '- 1 mBTC --> ' + mbtc2cnts(1) + ' c' + crrStr + '
';
_text += '- 1 mBTC --> ' + mbtc2sts(1) + ' sts
';
_text += '- 1 mBTC --> ' + mbtc2ubtc(1) + ' uBTC
';
_text += '- 1 uBTC ---> ' + ubtc2btc(1) + ' BTC
';
_text += '- 1 uBTC ---> ' + ubtc2crr(1) + ' ' + crrStr + '
';
_text += '- 1 uBTC ---> ' + ubtc2cnts(1) + ' c' + crrStr + '
';
_text += '- 1 uBTC ---> ' + ubtc2sts(1) + ' sts
';
_text += '- 1 uBTC ---> ' + ubtc2mbtc(1) + ' mBTC
';
return _text;
}
//Test 2
var bfTest2 = function(crrId) {
var crrStr = crrSymb(crrId);
var _text = '>>>>>>> TEST 2: EXAMPLE VALUES <<<<<<<
';
_text += '- 13 BTC --------------> ' + btc2crr(13) + ' ' + crrStr + '
';
_text += '- 13 BTC --------------> ' + btc2cnts(13) + ' c' + crrStr + '
';
_text += '- 13 BTC --------------> ' + btc2sts(13) + ' sts
';
_text += '- 13 BTC --------------> ' + btc2mbtc(13) + ' mBTC
';
_text += '- 13 BTC --------------> ' + btc2ubtc(13) + ' uBTC
';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2btc(736.6671) + ' BTC
';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2mbtc(736.6671) + ' mBTC
';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2ubtc(736.6671) + ' uBTC
';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2sts(736.6671) + ' sts
';
_text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2cnts(736.6671) + ' c' + crrStr + '
';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2btc(73666.71) + ' BTC
';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2mbtc(73666.71) + ' mBTC
';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2ubtc(73666.71) + ' uBTC
';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2sts(73666.71) + 'sts
';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2crr(73666.71) + crrStr + '
';
_text += '- 1300000000 sts ------> ' + sts2btc(1300000000) + ' BTC
';
_text += '- 1300000000 sts ------> ' + sts2mbtc(1300000000) + ' mBTC
';
_text += '- 1300000000 sts ------> ' + sts2ubtc(1300000000) + ' uBTC
';
_text += '- 1300000000 sts ------> ' + sts2crr(1300000000) + ' ' + crrStr + '
';
_text += '- 1300000000 sts ------> ' + sts2cnts(1300000000) + ' c' + crrStr + '
';
_text += '- 13000 mBTC --------> ' + mbtc2btc(13000) + ' BTC
';
_text += '- 13000 mBTC --------> ' + mbtc2crr(13000) + ' ' + crrStr + '
';
_text += '- 13000 mBTC --------> ' + mbtc2cnts(13000) + ' c' + crrStr + '
';
_text += '- 13000 mBTC --------> ' + mbtc2sts(13000) + ' sts
';
_text += '- 13000 mBTC --------> ' + mbtc2ubtc(13000) + ' uBTC
';
_text += '- 13000000 uBTC -----> ' + ubtc2btc(13000000) + ' BTC
';
_text += '- 13000000 uBTC -----> ' + ubtc2crr(13000000) + ' ' + crrStr + '
';
_text += '- 13000000 uBTC -----> ' + ubtc2cnts(13000000) + ' c' + crrStr + '
';
_text += '- 13000000 uBTC -----> ' + ubtc2sts(13000000) + ' sts
';
_text += '- 13000000 uBTC -----> ' + ubtc2mbtc(13000000) + ' mBTC
';
return _text;
}
//Launch
document.write( bfTest1('EUR') + bfTest2('EUR') + '
Useful? Please donate: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u
THNX!! ;)' );