JAVASCRIPT Bitcoin Conversion Functions v1.2


SUBMITTED BY: Guest

DATE: April 1, 2013, 2 a.m.

FORMAT: JavaScript

SIZE: 14.3 kB

HITS: 1956

  1. (function() {
  2. //--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  3. //JAVASCRIPT BITCOIN CONVERSION FUNCTIONS - v1.2
  4. //by Izzukay Bell - 01abr13 - siliconVega.com - c0nt4ct4r[at]gmail[dot]com
  5. //Nu in this version: Applied Ogig improvements and suggestions (Thanks to Ogig)
  6. //Please, donations here: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u
  7. //;)
  8. //God shave the Queen!!
  9. //--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
  10. btcConv = {};
  11. //CONFIG --------------------------------------------------------------
  12. //PUT HERE the BTC current or historic value (in your currency units)
  13. btcConv.val = 56.6667; //this is requested by * conversion functions
  14. //REQUESTED FUNCTIONS -------------------------------------------------
  15. //this is requested by ' conversion functions
  16. //isFloat
  17. //fm: http://www.forosdelweb.com/f13/como-saber-si-numero-decimal-entero-963718/#post4065830
  18. btcConv.isFloat = function(n) {
  19. return n % 1 != 0;
  20. }
  21. //this is requested by ' conversion functions
  22. //dlZ [deleteZeroes] (delete float at only zeroes case)
  23. btcConv.dlZ = function(n) {
  24. var o = ( parseInt(n.toString().split('.')[1]) == 0) ? parseInt(n) : n;
  25. return o;
  26. }
  27. //CONVERSION FUNCTIONS -------------------------------------------------
  28. //From BTCs to currency units ----------------- * '
  29. btcConv.btc2crr = function(btc) {
  30. return btcConv.isFloat(btc*btcConv.val) ? btcConv.dlZ( (btc*btcConv.val).toFixed(4) ) : btc*btcConv.val;
  31. }
  32. //From BTCs to currency cents ----------------- * '
  33. btcConv.btc2cnts = function(btc) {
  34. return btcConv.isFloat(btc*btcConv.val*100) ? btcConv.dlZ( (btc*btcConv.val*100).toFixed(2) ) : btc*btcConv.val*100;
  35. }
  36. //From BTCs to satoshis -----------------------
  37. btcConv.btc2sts = function(btc) {
  38. return Math.floor(btc*100000000);
  39. }
  40. //From BTCs to mBTCs -------------------------- '
  41. btcConv.btc2mbtc = function(btc) {
  42. return btcConv.isFloat(btc*1000) ? btcConv.dlZ( (btc*1000).toFixed(5) ) : btc*1000;
  43. }
  44. //From BTCs to uBTCs -------------------------- '
  45. btcConv.btc2ubtc = function(btc) {
  46. return btcConv.isFloat(btc*1000000) ? btcConv.dlZ( (btc*1000000).toFixed(2) ) : btc*1000000;
  47. }
  48. //From currency units to BTCs ----------------- * '
  49. btcConv.crr2btc = function(crr) {
  50. return btcConv.isFloat(crr/btcConv.val) ? btcConv.dlZ( (crr/btcConv.val).toFixed(8) ) : crr/btcConv.val;
  51. }
  52. //From currency units to mBTCs ---------------- * '
  53. btcConv.crr2mbtc = function(crr) {
  54. return btcConv.isFloat( (crr/btcConv.val)*1000 ) ? btcConv.dlZ( ( (crr/btcConv.val)*1000 ).toFixed(5) ) : (crr/btcConv.val)*1000;
  55. }
  56. //From currency units to uBTCs ---------------- * '
  57. btcConv.crr2ubtc = function(crr) {
  58. return btcConv.isFloat( (crr/btcConv.val)*1000000 ) ? btcConv.dlZ( ( (crr/btcConv.val)*1000000 ).toFixed(2) ) : (crr/btcConv.val)*1000000;
  59. }
  60. //From currency units to satoshis ------------- *
  61. btcConv.crr2sts = function(crr) {
  62. return Math.floor( (crr/btcConv.val)*100000000 );
  63. }
  64. //From currency units to currency cents ------- '
  65. btcConv.crr2cnts = function(crr) {
  66. return btcConv.isFloat(crr*100) ? btcConv.dlZ( (crr*100).toFixed(2) ) : crr*100;
  67. }
  68. //From currency cents to BTCs ----------------- * '
  69. btcConv.cnts2btc = function(cnts) {
  70. return btcConv.isFloat( (cnts/100)/btcConv.val ) ? btcConv.dlZ( ( (cnts/100)/btcConv.val ).toFixed(8) ) : (cnts/100)/btcConv.val;
  71. }
  72. //From currency cents to mBTCs ---------------- * '
  73. btcConv.cnts2mbtc = function(cnts) {
  74. return btcConv.isFloat( (cnts/btcConv.val)*10 ) ? btcConv.dlZ( ( (cnts/btcConv.val)*10 ).toFixed(5) ) : (cnts/btcConv.val)*10;
  75. }
  76. //From currency cents to uBTCs ---------------- * '
  77. btcConv.cnts2ubtc = function(cnts) {
  78. return btcConv.isFloat( (cnts/btcConv.val)*10000 ) ? btcConv.dlZ( ( (cnts/btcConv.val)*10000 ).toFixed(2) ) : (cnts/btcConv.val)*10000;
  79. }
  80. //From currency cents to satoshis ------------- *
  81. btcConv.cnts2sts = function(cnts) {
  82. return Math.floor( (cnts/btcConv.val)*1000000 );
  83. }
  84. //From currency cents to currency units ------- '
  85. btcConv.cnts2crr = function(cnts) {
  86. return btcConv.isFloat(cnts/100) ? btcConv.dlZ( (cnts/100).toFixed(4) ) : cnts/100;
  87. }
  88. //From satoshis to BTCs ----------------------- '
  89. btcConv.sts2btc = function(sts) {
  90. return btcConv.isFloat(sts/100000000) ? btcConv.dlZ( (sts/100000000).toFixed(8) ) : sts/100000000;
  91. }
  92. //From satoshis to mBTCs ---------------------- '
  93. btcConv.sts2mbtc = function(sts) {
  94. return btcConv.isFloat(sts/100000) ? btcConv.dlZ( (sts/100000).toFixed(5) ) : sts/100000;
  95. }
  96. //From satoshis to uBTCs ---------------------- '
  97. btcConv.sts2ubtc = function(sts) {
  98. return btcConv.isFloat(sts/100) ? btcConv.dlZ( (sts/100).toFixed(2) ) : sts/100;
  99. }
  100. //From satoshis to currency units ------------- * '
  101. btcConv.sts2crr = function(sts) {
  102. return btcConv.isFloat(sts*btcConv.val/100000000) ? btcConv.dlZ( (sts*btcConv.val/100000000).toFixed(4) ) : sts*btcConv.val/100000000;
  103. }
  104. //From satoshis to currency cents ------------- * '
  105. btcConv.sts2cnts = function(sts) {
  106. return btcConv.isFloat(sts*btcConv.val/1000000) ? btcConv.dlZ( (sts*btcConv.val/1000000).toFixed(2) ) : sts*btcConv.val/1000000;
  107. }
  108. //From mBTCs to currency units ---------------- * '
  109. btcConv.mbtc2crr = function(mbtc) {
  110. return btcConv.isFloat( (mbtc/1000)*btcConv.val ) ? btcConv.dlZ( ( (mbtc/1000)*btcConv.val ).toFixed(4) ) : (mbtc/1000)*btcConv.val;
  111. }
  112. //From mBTCs to currency cents ---------------- * '
  113. btcConv.mbtc2cnts = function(mbtc) {
  114. return btcConv.isFloat( (mbtc/10)*btcConv.val ) ? btcConv.dlZ( ( (mbtc/10)*btcConv.val ).toFixed(2) ) : (mbtc/10)*btcConv.val;
  115. }
  116. //From mBTCs to satoshis ----------------------
  117. btcConv.mbtc2sts = function(mbtc) {
  118. return Math.floor(mbtc*100000);
  119. }
  120. //From mBTCs to BTCs -------------------------- '
  121. btcConv.mbtc2btc = function(mbtc) {
  122. return btcConv.isFloat(mbtc/1000) ? btcConv.dlZ( (mbtc/1000).toFixed(8) ) : mbtc/1000;
  123. }
  124. //From mBTCs to uBTCs ------------------------- '
  125. btcConv.mbtc2ubtc = function(mbtc) {
  126. return btcConv.isFloat(mbtc*1000) ? btcConv.dlZ( (mbtc*1000).toFixed(2) ) : mbtc*1000;
  127. }
  128. //From uBTCs to currency units ---------------- * '
  129. btcConv.ubtc2crr = function(ubtc) {
  130. return btcConv.isFloat( (ubtc/1000000)*btcConv.val ) ? btcConv.dlZ( ( (ubtc/1000000)*btcConv.val ).toFixed(4) ) : (ubtc/1000000)*btcConv.val;
  131. }
  132. //From uBTCs to currency cents ---------------- * '
  133. btcConv.ubtc2cnts = function(ubtc) {
  134. return btcConv.isFloat( (ubtc/10000)*btcConv.val ) ? btcConv.dlZ( ( (ubtc/10000)*btcConv.val ).toFixed(2) ) : (ubtc/10000)*btcConv.val;
  135. }
  136. //From uBTCs to satoshis ----------------------
  137. btcConv.ubtc2sts = function(ubtc) {
  138. return Math.floor(ubtc*100);
  139. }
  140. //From uBTCs to mBTCs ------------------------- '
  141. btcConv.ubtc2mbtc = function(ubtc) {
  142. return btcConv.isFloat(ubtc/1000) ? btcConv.dlZ( (ubtc/1000).toFixed(5) ) : ubtc/1000;
  143. }
  144. //From uBTCs to BTCs -------------------------- '
  145. btcConv.ubtc2btc = function(ubtc) {
  146. return btcConv.isFloat(ubtc/1000000) ? btcConv.dlZ( (ubtc/1000000).toFixed(8) ) : ubtc/1000000;
  147. }
  148. }());
  149. //TEST FUNCTIONS -------------------------------------------------------
  150. //Catch currency symbol
  151. var crrSymb = function(crr) {
  152. if(crr == 'EUR') return String.fromCharCode(8364);
  153. if(crr == 'USD') return String.fromCharCode(36);
  154. if(crr == 'GBP') return String.fromCharCode(163);
  155. if(crr == 'JPY') return String.fromCharCode(165);
  156. return crr;
  157. }
  158. //Test 1
  159. var bfTest1 = function(crrId) {
  160. var crrStr = crrSymb(crrId);
  161. var _text = '>>>>>>> TEST 1: UNITARY VALUES <<<<<<<<br/>';
  162. _text += '- 1 BTC ---> ' + btcConv.btc2crr(1) + ' ' + crrStr + '<br/>';
  163. _text += '- 1 BTC ---> ' + btcConv.btc2cnts(1) + ' c' + crrStr + '<br/>';
  164. _text += '- 1 BTC ---> ' + btcConv.btc2sts(1) + ' sts<br/>';
  165. _text += '- 1 BTC ---> ' + btcConv.btc2mbtc(1) + ' mBTC<br/>';
  166. _text += '- 1 BTC ---> ' + btcConv.btc2ubtc(1) + ' uBTC<br/>';
  167. _text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2btc(1) + ' BTC<br/>';
  168. _text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2mbtc(1) + ' mBTC<br/>';
  169. _text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2ubtc(1) + ' uBTC<br/>';
  170. _text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2sts(1) + ' sts<br/>';
  171. _text += '- 1 ' + crrStr + ' -------> ' + btcConv.crr2cnts(1) + ' c' + crrStr + '<br/>';
  172. _text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2btc(1) + ' BTC<br/>';
  173. _text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2mbtc(1) + ' mBTC<br/>';
  174. _text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2ubtc(1) + ' uBTC<br/>';
  175. _text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2sts(1) + 'sts<br/>';
  176. _text += '- 1 c' + crrStr + ' ------> ' + btcConv.cnts2crr(1) + crrStr + '<br/>';
  177. _text += '- 1 sts ------> ' + btcConv.sts2btc(1) + ' BTC<br/>';
  178. _text += '- 1 sts ------> ' + btcConv.sts2mbtc(1) + ' mBTC<br/>';
  179. _text += '- 1 sts ------> ' + btcConv.sts2ubtc(1) + ' uBTC<br/>';
  180. _text += '- 1 sts ------> ' + btcConv.sts2crr(1) + ' ' + crrStr + '<br/>';
  181. _text += '- 1 sts ------> ' + btcConv.sts2cnts(1) + ' c' + crrStr + '<br/>';
  182. _text += '- 1 mBTC --> ' + btcConv.mbtc2btc(1) + ' BTC<br/>';
  183. _text += '- 1 mBTC --> ' + btcConv.mbtc2crr(1) + ' ' + crrStr + '<br/>';
  184. _text += '- 1 mBTC --> ' + btcConv.mbtc2cnts(1) + ' c' + crrStr + '<br/>';
  185. _text += '- 1 mBTC --> ' + btcConv.mbtc2sts(1) + ' sts<br/>';
  186. _text += '- 1 mBTC --> ' + btcConv.mbtc2ubtc(1) + ' uBTC<br/>';
  187. _text += '- 1 uBTC ---> ' + btcConv.ubtc2btc(1) + ' BTC<br/>';
  188. _text += '- 1 uBTC ---> ' + btcConv.ubtc2crr(1) + ' ' + crrStr + '<br/>';
  189. _text += '- 1 uBTC ---> ' + btcConv.ubtc2cnts(1) + ' c' + crrStr + '<br/>';
  190. _text += '- 1 uBTC ---> ' + btcConv.ubtc2sts(1) + ' sts<br/>';
  191. _text += '- 1 uBTC ---> ' + btcConv.ubtc2mbtc(1) + ' mBTC<br/><br/>';
  192. return _text;
  193. }
  194. //Test 2
  195. var bfTest2 = function(crrId) {
  196. var crrStr = crrSymb(crrId);
  197. var _text = '>>>>>>> TEST 2: EXAMPLE VALUES <<<<<<<<br/>';
  198. _text += '- 13 BTC --------------> ' + btcConv.btc2crr(13) + ' ' + crrStr + '<br/>';
  199. _text += '- 13 BTC --------------> ' + btcConv.btc2cnts(13) + ' c' + crrStr + '<br/>';
  200. _text += '- 13 BTC --------------> ' + btcConv.btc2sts(13) + ' sts<br/>';
  201. _text += '- 13 BTC --------------> ' + btcConv.btc2mbtc(13) + ' mBTC<br/>';
  202. _text += '- 13 BTC --------------> ' + btcConv.btc2ubtc(13) + ' uBTC<br/>';
  203. _text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2btc(736.6671) + ' BTC<br/>';
  204. _text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2mbtc(736.6671) + ' mBTC<br/>';
  205. _text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2ubtc(736.6671) + ' uBTC<br/>';
  206. _text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2sts(736.6671) + ' sts<br/>';
  207. _text += '- 736.6671 ' + crrStr + ' -----------> ' + btcConv.crr2cnts(736.6671) + ' c' + crrStr + '<br/>';
  208. _text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2btc(73666.71) + ' BTC<br/>';
  209. _text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2mbtc(73666.71) + ' mBTC<br/>';
  210. _text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2ubtc(73666.71) + ' uBTC<br/>';
  211. _text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2sts(73666.71) + 'sts<br/>';
  212. _text += '- 73666.71 c' + crrStr + ' ----------> ' + btcConv.cnts2crr(73666.71) + crrStr + '<br/>';
  213. _text += '- 1300000000 sts ------> ' + btcConv.sts2btc(1300000000) + ' BTC<br/>';
  214. _text += '- 1300000000 sts ------> ' + btcConv.sts2mbtc(1300000000) + ' mBTC<br/>';
  215. _text += '- 1300000000 sts ------> ' + btcConv.sts2ubtc(1300000000) + ' uBTC<br/>';
  216. _text += '- 1300000000 sts ------> ' + btcConv.sts2crr(1300000000) + ' ' + crrStr + '<br/>';
  217. _text += '- 1300000000 sts ------> ' + btcConv.sts2cnts(1300000000) + ' c' + crrStr + '<br/>';
  218. _text += '- 13000 mBTC --------> ' + btcConv.mbtc2btc(13000) + ' BTC<br/>';
  219. _text += '- 13000 mBTC --------> ' + btcConv.mbtc2crr(13000) + ' ' + crrStr + '<br/>';
  220. _text += '- 13000 mBTC --------> ' + btcConv.mbtc2cnts(13000) + ' c' + crrStr + '<br/>';
  221. _text += '- 13000 mBTC --------> ' + btcConv.mbtc2sts(13000) + ' sts<br/>';
  222. _text += '- 13000 mBTC --------> ' + btcConv.mbtc2ubtc(13000) + ' uBTC<br/>';
  223. _text += '- 13000000 uBTC -----> ' + btcConv.ubtc2btc(13000000) + ' BTC<br/>';
  224. _text += '- 13000000 uBTC -----> ' + btcConv.ubtc2crr(13000000) + ' ' + crrStr + '<br/>';
  225. _text += '- 13000000 uBTC -----> ' + btcConv.ubtc2cnts(13000000) + ' c' + crrStr + '<br/>';
  226. _text += '- 13000000 uBTC -----> ' + btcConv.ubtc2sts(13000000) + ' sts<br/>';
  227. _text += '- 13000000 uBTC -----> ' + btcConv.ubtc2mbtc(13000000) + ' mBTC<br/><br/>';
  228. return _text;
  229. }
  230. //Launch
  231. document.write( bfTest1('EUR') + bfTest2('EUR') + '<br/>Useful? Please donate: 175o6gdTXpFo5FKqbqcUPSZkZ1fSMxSK7u<br/>THNX!! ;)' );

comments powered by Disqus