Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. // this is my code so far:
  2.  
  3. var currency = doc.stock.exchange.currency; //this is how I get the currency
  4. var formatNumberNat = val.toLocaleString(
  5. 'en-US', {
  6. style: 'currency',
  7. currency: currency
  8. }
  9. );
  10. return formatNumberNat; /* €90,102,409,320.00 */
  11.  
  12. function nFormatter(num, digits) {
  13. var si = [
  14. { value: 1E18, symbol: "E" },
  15. { value: 1E15, symbol: "P" },
  16. { value: 1E12, symbol: "T" },
  17. { value: 1E9, symbol: "G" },
  18. { value: 1E6, symbol: "M" },
  19. { value: 1E3, symbol: "k" }
  20. ], i;
  21. for (i = 0; i < si.length; i++) {
  22. if (num >= si[i].value) {
  23. return (num / si[i].value).toFixed(digits).replace(/.0+$|(.[0-9]*[1-9])0+$/, "$1") + si[i].symbol;
  24. }
  25. }
  26. return num.toString();
  27. }
  28. console.log(nFormatter(123, 1)); // 123
  29. console.log(nFormatter(1234, 1)); // 1.2k
  30. console.log(nFormatter(100000000, 1)); // 100M
  31. console.log(nFormatter(299792458, 1)); // 299.8M
  32. console.log(nFormatter(759878, 1)); // 759.9k
  33. console.log(nFormatter(759878, 0)); // 760k
  34.  
  35. function formatB(n) {
  36. if (n > 999999999)
  37. return "$" + (n/1000000000).toFixed(2) + "B";
  38. else if(n > 999999)
  39. return "$" + (n/1000000).toFixed(2) + "M";
  40. else
  41. return n;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement