JAVASCRIPT Bitcoin Conversion Functions v1.1


SUBMITTED BY: Guest

DATE: March 30, 2013, 5:26 p.m.

FORMAT: JavaScript

SIZE: 13.7 kB

HITS: 2598

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

comments powered by Disqus