Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MYR Currency Rounding

Looking for help in rounding the money to the nearest 0.05 in js language.

Input & Expected Output:

1.10 => 1.10
1.11 => 1.10 (round down)
1.12 => 1.10 (round down)
1.13 => 1.15 (round up)
1.14 => 1.15 (round up)
1.15 => 1.15
1.16 => 1.15 (round down)
1.17 => 1.15 (round down)
1.18 => 1.20 (round up)
1.19 => 1.20 (round up)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

From: https://www.bnm.gov.my/misc/-/asset_publisher/2BOPbOBfILtL/content/frequently-asked-questions-faqs-on-rounding-mechanism

>Solution :

You could take an offset and take a multiple floored value.

If you need zeroes at the end take .toFixed(2).

const format = f => Math.floor((f + 0.025) * 20) / 20;

console.log([1.10, 1.11, 1.12, 1.13, 1.14, 1.15, 1.16, 1.17, 1.18, 1.19].map(format));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading