You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rounding.js 3.8 KiB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. context("Rounding behaviour", () => {
  2. before(() => {
  3. cy.login();
  4. cy.visit("/app/");
  5. });
  6. it("Commercial Rounding", () => {
  7. cy.window()
  8. .its("flt")
  9. .then((flt) => {
  10. let rounding_method = "Commercial Rounding";
  11. expect(flt("0.5", 0, null, rounding_method)).eq(1);
  12. expect(flt("0.3", null, null, rounding_method)).eq(0.3);
  13. expect(flt("1.5", 0, null, rounding_method)).eq(2);
  14. // positive rounding to integers
  15. expect(flt(0.4, 0, null, rounding_method)).eq(0);
  16. expect(flt(0.5, 0, null, rounding_method)).eq(1);
  17. expect(flt(1.455, 0, null, rounding_method)).eq(1);
  18. expect(flt(1.5, 0, null, rounding_method)).eq(2);
  19. // negative rounding to integers
  20. expect(flt(-0.5, 0, null, rounding_method)).eq(-1);
  21. expect(flt(-1.5, 0, null, rounding_method)).eq(-2);
  22. // negative precision i.e. round to nearest 10th
  23. expect(flt(123, -1, null, rounding_method)).eq(120);
  24. expect(flt(125, -1, null, rounding_method)).eq(130);
  25. expect(flt(134.45, -1, null, rounding_method)).eq(130);
  26. expect(flt(135, -1, null, rounding_method)).eq(140);
  27. // positive multiple digit rounding
  28. expect(flt(1.25, 1, null, rounding_method)).eq(1.3);
  29. expect(flt(0.15, 1, null, rounding_method)).eq(0.2);
  30. expect(flt(2.675, 2, null, rounding_method)).eq(2.68);
  31. // negative multiple digit rounding
  32. expect(flt(-1.25, 1, null, rounding_method)).eq(-1.3);
  33. expect(flt(-0.15, 1, null, rounding_method)).eq(-0.2);
  34. });
  35. });
  36. it("Banker's Rounding", () => {
  37. cy.window()
  38. .its("flt")
  39. .then((flt) => {
  40. let rounding_method = "Banker's Rounding";
  41. expect(flt("0.5", 0, null, rounding_method)).eq(0);
  42. expect(flt("0.3", null, null, rounding_method)).eq(0.3);
  43. expect(flt("1.5", 0, null, rounding_method)).eq(2);
  44. // positive rounding to integers
  45. expect(flt(0.4, 0, null, rounding_method)).eq(0);
  46. expect(flt(0.5, 0, null, rounding_method)).eq(0);
  47. expect(flt(1.455, 0, null, rounding_method)).eq(1);
  48. expect(flt(1.5, 0, null, rounding_method)).eq(2);
  49. // negative rounding to integers
  50. expect(flt(-0.5, 0, null, rounding_method)).eq(0);
  51. expect(flt(-1.5, 0, null, rounding_method)).eq(-2);
  52. // negative precision i.e. round to nearest 10th
  53. expect(flt(123, -1, null, rounding_method)).eq(120);
  54. expect(flt(125, -1, null, rounding_method)).eq(120);
  55. expect(flt(134.45, -1, null, rounding_method)).eq(130);
  56. expect(flt(135, -1, null, rounding_method)).eq(140);
  57. // positive multiple digit rounding
  58. expect(flt(1.25, 1, null, rounding_method)).eq(1.2);
  59. expect(flt(0.15, 1, null, rounding_method)).eq(0.2);
  60. expect(flt(2.675, 2, null, rounding_method)).eq(2.68);
  61. expect(flt(-2.675, 2, null, rounding_method)).eq(-2.68);
  62. // negative multiple digit rounding
  63. expect(flt(-1.25, 1, null, rounding_method)).eq(-1.2);
  64. expect(flt(-0.15, 1, null, rounding_method)).eq(-0.2);
  65. // Nearest number and not even (the default behaviour)
  66. expect(flt(0.5, 0, null, rounding_method)).eq(0);
  67. expect(flt(1.5, 0, null, rounding_method)).eq(2);
  68. expect(flt(2.5, 0, null, rounding_method)).eq(2);
  69. expect(flt(3.5, 0, null, rounding_method)).eq(4);
  70. expect(flt(0.05, 1, null, rounding_method)).eq(0.0);
  71. expect(flt(1.15, 1, null, rounding_method)).eq(1.2);
  72. expect(flt(2.25, 1, null, rounding_method)).eq(2.2);
  73. expect(flt(3.35, 1, null, rounding_method)).eq(3.4);
  74. expect(flt(-0.5, 0, null, rounding_method)).eq(0);
  75. expect(flt(-1.5, 0, null, rounding_method)).eq(-2);
  76. expect(flt(-2.5, 0, null, rounding_method)).eq(-2);
  77. expect(flt(-3.5, 0, null, rounding_method)).eq(-4);
  78. expect(flt(-0.05, 1, null, rounding_method)).eq(0.0);
  79. expect(flt(-1.15, 1, null, rounding_method)).eq(-1.2);
  80. expect(flt(-2.25, 1, null, rounding_method)).eq(-2.2);
  81. expect(flt(-3.35, 1, null, rounding_method)).eq(-3.4);
  82. });
  83. });
  84. });