JAVASCRIPT Bitcoin Conversion Functions


SUBMITTED BY: Guest

DATE: March 23, 2013, 1:34 a.m.

FORMAT: JavaScript

SIZE: 7.1 kB

HITS: 2752

  1. //--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  2. //JAVASCRIPT BITCOIN CONVERSION FUNCTIONS - v1.0
  3. //by Izzukay Bell - 23mar13 - siliconVega.com - c0nt4ct4r[at]gmail[dot]com
  4. //Please, donations here: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u
  5. //;)
  6. //God shave the Queen!!
  7. //--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  8. //CONFIG --------------------------------------------------------------
  9. //PUT HERE the BTC current or historic value (in your currency units)
  10. var val = 56.6667; //this is requested by * conversion functions
  11. //REQUESTED FUNCTIONS -------------------------------------------------
  12. //this is requested by all conversion functions
  13. //tfxp [toFixedPlus] (scientific notation off)
  14. //fm: http://es.softuses.com/208551
  15. function tfxp(x) {
  16. if (Math.abs(x) < 1.0) {
  17. var e = parseInt(x.toString().split('e-')[1]);
  18. if (e) {
  19. x *= Math.pow(10,e-1);
  20. x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
  21. }
  22. } else {
  23. var e = parseInt(x.toString().split('+')[1]);
  24. if (e > 20) {
  25. e -= 20;
  26. x /= Math.pow(10,e);
  27. x += (new Array(e+1)).join('0');
  28. }
  29. }
  30. return x;
  31. }
  32. //this is requested by ' conversion functions
  33. //isFloat
  34. //fm: http://www.forosdelweb.com/f13/como-saber-si-numero-decimal-entero-963718/#post4065830
  35. function isFloat(n) {
  36. return n % 1 != 0;
  37. }
  38. //this is requested by ' conversion functions
  39. //dlZ [deleteZeroes] (delete float at only zeroes case)
  40. var dlZ = function(n) {
  41. var o = ( parseInt(n.toString().split('.')[1]) == 0) ? parseInt(n) : n;
  42. return o;
  43. }
  44. //CONVERSION FUNCTIONS -------------------------------------------------
  45. //From BTCs to currency units ----------------- * '
  46. var btc2crr = function(btc) {
  47. return isFloat(btc*val) ? dlZ( tfxp( (btc*val).toFixed(4) ) ) : btc*val;
  48. }
  49. //From BTCs to currency cents ----------------- * '
  50. var btc2cnts = function(btc) {
  51. return isFloat( (btc*val)*100 ) ? dlZ( tfxp( ( (btc*val)*100 ).toFixed(2) ) ) : (btc*val)*100;
  52. }
  53. //From BTCs to satoshis -----------------------
  54. var btc2sts = function(btc) {
  55. return Math.floor(btc*100000000);
  56. }
  57. //From currency units to BTCs ----------------- * '
  58. var crr2btc = function(crr) {
  59. return isFloat(crr/val) ? dlZ( tfxp( (crr/val).toFixed(8) ) ) : crr/val;
  60. }
  61. //From currency units to satoshis ------------- *
  62. var crr2sts = function(crr) {
  63. return Math.floor( (crr/val)*100000000 );
  64. }
  65. //From currency units to currency cents ------- '
  66. var crr2cnts = function(crr) {
  67. return isFloat(crr*100) ? dlZ( tfxp( (crr*100).toFixed(2) ) ) : crr*100;
  68. }
  69. //From currency cents to BTCs ----------------- * '
  70. var cnts2btc = function(cnts) {
  71. return isFloat( (cnts/100)/val ) ? dlZ( tfxp( ( (cnts/100)/val ).toFixed(8) ) ) : (cnts/100)/val;
  72. }
  73. //From currency cents to satoshis ------------- *
  74. var cnts2sts = function(cnts) {
  75. return Math.floor( ((cnts/100)/val)*100000000 );
  76. }
  77. //From currency cents to currency units ------- '
  78. var cnts2crr = function(cnts) {
  79. return isFloat(cnts/100) ? dlZ( tfxp( (cnts/100).toFixed(4) ) ) : cnts/100;
  80. }
  81. //From satoshis to BTCs ----------------------- '
  82. var sts2btc = function(sts) {
  83. return isFloat(sts/100000000) ? dlZ( tfxp( (sts/100000000).toFixed(8) ) ) : sts/100000000;
  84. }
  85. //From satoshis to currency units ------------- * '
  86. var sts2crr = function(sts) {
  87. return isFloat(sts*val/100000000) ? dlZ( tfxp( (sts*val/100000000).toFixed(4) ) ) : sts*val/100000000;
  88. }
  89. //From satoshis to currency cents ------------- * '
  90. var sts2cnts = function(sts) {
  91. return isFloat(sts*val*100/100000000) ? dlZ( tfxp( (sts*val*100/100000000).toFixed(2) ) ) : sts*val*100/100000000;
  92. }
  93. //TEST FUNCTIONS -------------------------------------------------------
  94. //Catch currency symbol
  95. var crrSymb = function(crr) {
  96. if(crr == 'EUR') return String.fromCharCode(8364);
  97. if(crr == 'USD') return String.fromCharCode(36);
  98. if(crr == 'GBP') return String.fromCharCode(163);
  99. if(crr == 'JPY') return String.fromCharCode(165);
  100. return crr;
  101. }
  102. //Test 1
  103. var bfTest1 = function(crrId) {
  104. var crrStr = crrSymb(crrId);
  105. var _text = '>>>>>>> TEST 1: UNITARY VALUES <<<<<<<<br/>';
  106. _text += '- 1 BTC ---> ' + btc2crr(1) + ' ' + crrStr + '<br/>';
  107. _text += '- 1 BTC ---> ' + btc2cnts(1) + ' c' + crrStr + '<br/>';
  108. _text += '- 1 BTC ---> ' + btc2sts(1) + ' sts<br/>';
  109. _text += '- 1 ' + crrStr + ' -------> ' + crr2btc(1) + ' BTC<br/>';
  110. _text += '- 1 ' + crrStr + ' -------> ' + crr2sts(1) + ' sts<br/>';
  111. _text += '- 1 ' + crrStr + ' -------> ' + crr2cnts(1) + ' c' + crrStr + '<br/>';
  112. _text += '- 1 c' + crrStr + ' ------> ' + cnts2btc(1) + ' BTC<br/>';
  113. _text += '- 1 c' + crrStr + ' ------> ' + cnts2sts(1) + 'sts<br/>';
  114. _text += '- 1 c' + crrStr + ' ------> ' + cnts2crr(1) + crrStr + '<br/>';
  115. _text += '- 1 sts ------> ' + sts2btc(1) + ' BTC<br/>';
  116. _text += '- 1 sts ------> ' + sts2crr(1) + ' ' + crrStr + '<br/>';
  117. _text += '- 1 sts ------> ' + sts2cnts(1) + ' c' + crrStr + '<br/><br/>';
  118. return _text;
  119. }
  120. //Test 2
  121. var bfTest2 = function(crrId) {
  122. var crrStr = crrSymb(crrId);
  123. var _text = '>>>>>>> TEST 2: EXAMPLE VALUES <<<<<<<<br/>';
  124. _text += '- 13 BTC --------------> ' + btc2crr(13) + ' ' + crrStr + '<br/>';
  125. _text += '- 13 BTC --------------> ' + btc2cnts(13) + ' c' + crrStr + '<br/>';
  126. _text += '- 13 BTC --------------> ' + btc2sts(13) + ' sts<br/>';
  127. _text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2btc(736.6671) + ' BTC<br/>';
  128. _text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2sts(736.6671) + ' sts<br/>';
  129. _text += '- 736.6671 ' + crrStr + ' -----------> ' + crr2cnts(736.6671) + ' c' + crrStr + '<br/>';
  130. _text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2btc(73666.71) + ' BTC<br/>';
  131. _text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2sts(73666.71) + 'sts<br/>';
  132. _text += '- 73666.71 c' + crrStr + ' ----------> ' + cnts2crr(73666.71) + crrStr + '<br/>';
  133. _text += '- 1300000000 sts ------> ' + sts2btc(1300000000) + ' BTC<br/>';
  134. _text += '- 1300000000 sts ------> ' + sts2crr(1300000000) + ' ' + crrStr + '<br/>';
  135. _text += '- 1300000000 sts ------> ' + sts2cnts(1300000000) + ' c' + crrStr + '<br/>';
  136. return _text;
  137. }
  138. //Launch
  139. document.write( bfTest1('EUR') + bfTest2('EUR') + '<br/>Useful? Please donate: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u<br/>THNX!! ;)' );

comments powered by Disqus