//--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
//JAVASCRIPT BITCOIN CONVERSION FUNCTIONS - v1.0
//by Izzukay Bell - 23mar13 - siliconVega.com - c0nt4ct4r[at]gmail[dot]com
//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 all 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 currency units to BTCs ----------------- * '
var crr2btc = function(crr) {
return isFloat(crr/val) ? dlZ( tfxp( (crr/val).toFixed(8) ) ) : crr/val;
}
//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 satoshis ------------- *
var cnts2sts = function(cnts) {
return Math.floor( ((cnts/100)/val)*100000000 );
}
//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 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*100/100000000) ? dlZ( tfxp( (sts*val*100/100000000).toFixed(2) ) ) : sts*val*100/100000000;
}
//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 ' + crrStr + ' -------> ' + crr2btc(1) + ' BTC
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2sts(1) + ' sts
';
_text += '- 1 ' + crrStr + ' -------> ' + crr2cnts(1) + ' c' + crrStr + '
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2btc(1) + ' BTC
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2sts(1) + 'sts
';
_text += '- 1 c' + crrStr + ' ------> ' + cnts2crr(1) + crrStr + '
';
_text += '- 1 sts ------> ' + sts2btc(1) + ' BTC
';
_text += '- 1 sts ------> ' + sts2crr(1) + ' ' + crrStr + '
';
_text += '- 1 sts ------> ' + sts2cnts(1) + ' c' + crrStr + '
';
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 += '- 736.6671 ' + crrStr + ' -----------> ' + crr2btc(736.6671) + ' BTC
';
_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 + ' ----------> ' + cnts2sts(73666.71) + 'sts
';
_text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2crr(73666.71) + crrStr + '
';
_text += '- 1300000000 sts ------> ' + sts2btc(1300000000) + ' BTC
';
_text += '- 1300000000 sts ------> ' + sts2crr(1300000000) + ' ' + crrStr + '
';
_text += '- 1300000000 sts ------> ' + sts2cnts(1300000000) + ' c' + crrStr + '
';
return _text;
}
//Launch
document.write( bfTest1('EUR') + bfTest2('EUR') + '
Useful? Please donate: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u
THNX!! ;)' );