[text] asd

Viewer

  1. document.write('const { abs, floor, round } = Math');
  2. document.write('const CENT = 0.005');
  3. document.write('const TEN = 10');
  4. document.write('const HUNDRED = 100');
  5. document.write('const THOUSAND = 1000');
  6. document.write('const MILLION = 1000000');
  7. document.write('const BILLION = 1000000000');
  8. document.write('const TRILLION = 1000000000000');
  9. document.write('');
  10. document.write('function truncate(number: number, base: number) {');
  11. document.write('  const divided = number / base');
  12. document.write('  return divided < HUNDRED ? floor(divided * 10) / 10 : floor(divided)');
  13. document.write('}');
  14. document.write('');
  15. document.write('function rounded(number: number, base: number) {');
  16. document.write('  const divided = number / base');
  17. document.write('  return divided < HUNDRED ? round(divided * 10) / 10 : round(divided)');
  18. document.write('}');
  19. document.write('');
  20. document.write('export function abbreviateCurrency(number: number): string {');
  21. document.write('  if (typeof number !== \'number\') {');
  22. document.write('    const parsed = parseFloat(number)');
  23. document.write('    if (isNaN(parsed)) throw Error(`Expected input "${number}" to be a number`)');
  24. document.write('    number = parsed');
  25. document.write('  }');
  26. document.write('  const isNegative = number < 0');
  27. document.write('  const absoluteValue = abs(number)');
  28. document.write('  let abbreviated: string');
  29. document.write('');
  30. document.write('  if (absoluteValue < CENT) return \'$0\'');
  31. document.write('  else if (absoluteValue < TEN) abbreviated = absoluteValue.toFixed(2)');
  32. document.write('  else if (absoluteValue < THOUSAND) abbreviated = absoluteValue.toFixed(0)');
  33. document.write('  else if (absoluteValue < MILLION) abbreviated = truncate(absoluteValue, THOUSAND) + \'K\'');
  34. document.write('  else if (absoluteValue < BILLION) abbreviated = truncate(absoluteValue, MILLION) + \'M\'');
  35. document.write('  else if (absoluteValue < TRILLION) abbreviated = truncate(absoluteValue, BILLION) + \'B\'');
  36. document.write('  else abbreviated = truncate(absoluteValue, TRILLION) + \'T\'');
  37. document.write('');
  38. document.write('  return isNegative ? \'-$\' + abbreviated : \'$\' + abbreviated');
  39. document.write('}');
  40. document.write('');
  41. document.write('export function abbreviateCurrencyRounded(number: number): string {');
  42. document.write('  if (typeof number !== \'number\') {');
  43. document.write('    const parsed = parseFloat(number)');
  44. document.write('    if (isNaN(parsed)) throw Error(`Expected input "${number}" to be a number`)');
  45. document.write('    number = parsed');
  46. document.write('  }');
  47. document.write('  const isNegative = number < 0');
  48. document.write('  const absoluteValue = abs(number)');
  49. document.write('  let abbreviated: string');
  50. document.write('');
  51. document.write('  if (absoluteValue < CENT) return \'$0\'');
  52. document.write('  else if (absoluteValue < TEN) abbreviated = absoluteValue.toFixed(2)');
  53. document.write('  else if (absoluteValue < THOUSAND) abbreviated = absoluteValue.toFixed(0)');
  54. document.write('  else if (absoluteValue < MILLION) abbreviated = rounded(absoluteValue, THOUSAND) + \'K\'');
  55. document.write('  else if (absoluteValue < BILLION) abbreviated = rounded(absoluteValue, MILLION) + \'M\'');
  56. document.write('  else if (absoluteValue < TRILLION) abbreviated = rounded(absoluteValue, BILLION) + \'B\'');
  57. document.write('  else abbreviated = rounded(absoluteValue, TRILLION) + \'T\'');
  58. document.write('');
  59. document.write('  return isNegative ? \'-$\' + abbreviated : \'$\' + abbreviated');
  60. document.write('}');
  61. document.write('');
  62. document.write('function truncateTwoDecimal(number: number, base: number) {');
  63. document.write('  const divided = number / base');
  64. document.write('  return divided < HUNDRED ? ((divided * 10) / 10).toFixed(2) : divided.toFixed(2)');
  65. document.write('}');
  66. document.write('');
  67. document.write('export function abbreviateCurrencyTwoDecimal(number: number): string {');
  68. document.write('  if (typeof number !== \'number\') {');
  69. document.write('    const parsed = parseFloat(number)');
  70. document.write('    if (isNaN(parsed)) throw Error(`Expected input "${number}" to be a number`)');
  71. document.write('    number = parsed');
  72. document.write('  }');
  73. document.write('  const isNegative = number < 0');
  74. document.write('  const absoluteValue = abs(number)');
  75. document.write('  let abbreviated: string');
  76. document.write('');
  77. document.write('  if (absoluteValue < CENT) return \'$0\'');
  78. document.write('  else if (absoluteValue < TEN) abbreviated = absoluteValue.toFixed(2)');
  79. document.write('  else if (absoluteValue < THOUSAND) abbreviated = absoluteValue.toFixed(0)');
  80. document.write('  else if (absoluteValue < MILLION) abbreviated = truncateTwoDecimal(absoluteValue, THOUSAND) + \'K\'');
  81. document.write('  else if (absoluteValue < BILLION) abbreviated = truncateTwoDecimal(absoluteValue, MILLION) + \'M\'');
  82. document.write('  else if (absoluteValue < TRILLION) abbreviated = truncateTwoDecimal(absoluteValue, BILLION) + \'B\'');
  83. document.write('  else abbreviated = truncateTwoDecimal(absoluteValue, TRILLION) + \'T\'');
  84. document.write('');
  85. document.write('  return isNegative ? \'-$\' + abbreviated : \'$\' + abbreviated');
  86. document.write('}');
  87.  

Editor

You can edit this paste and save as new:


File Description
  • asd
  • Paste Code
  • 24 Sep-2021
  • 5.13 Kb
You can Share it: