Forked from petrosmm/MidpointRounding.AwayFromZero.js
Last active
August 23, 2023 07:47
-
-
Save adrian-green/e54c71e84b1af0786b6c811fba089d96 to your computer and use it in GitHub Desktop.
A function to assist in MidpointRounding.AwayFromZero in Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Math.RoundAwayFromZero = function round(num, decimalPlaces) { | |
const d = decimalPlaces || 2; | |
const m = Math.pow(10, d); | |
const n = +(d ? num * m : num).toFixed(8); | |
const i = Math.ceil(n), f = n - i; | |
const e = 1e-8; | |
const r = (f > 0.5 - e && f < 0.5 + e) ? | |
((i % 2 === 0) ? i : i + 1) : Math.round(n); | |
return d ? r / m : r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Program.cs
Math_Round_AwayFromZero.js