toFraction.js 701 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. Fraction.js v5.0.0 10/1/2024
  3. https://raw.org/article/rational-numbers-in-javascript/
  4. Copyright (c) 2024, Robert Eisele (https://raw.org/)
  5. Licensed under the MIT license.
  6. */
  7. const Fraction = require('fraction.js');
  8. function toFraction(frac) {
  9. var map = {
  10. '1:4': "¼",
  11. '1:2': "½",
  12. '3:4': "¾",
  13. '1:7': "⅐",
  14. '1:9': "⅑",
  15. '1:10': "⅒",
  16. '1:3': "⅓",
  17. '2:3': "⅔",
  18. '1:5': "⅕",
  19. '2:5': "⅖",
  20. '3:5': "⅗",
  21. '4:5': "⅘",
  22. '1:6': "⅙",
  23. '5:6': "⅚",
  24. '1:8': "⅛",
  25. '3:8': "⅜",
  26. '5:8': "⅝",
  27. '7:8': "⅞"
  28. };
  29. return map[frac.n + ":" + frac.d] || frac.toFraction(false);
  30. }
  31. console.log(toFraction(Fraction(0.25))); // ¼