Last active
November 3, 2021 02:16
-
-
Save choyer/f239952499a4cdc772a042a1d377659e to your computer and use it in GitHub Desktop.
AWK/GAWK function to round float numbers to specified decimal place
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
function round(x, p, s) { | |
# DESC: Rounds float to the number of decimals specified | |
# ARGS: x (required) - number to round. float valid | |
# p - decimal place value rounded to | |
# defaults 1 if unspecified. | |
# RETURN: rounded float | |
# USAGE: round(5.255,2) | |
# NOTES: Handles positive/negative floats & returns ints | |
p = 10^p | |
s = 1 | |
if (x < 0) { x = -x; s = -1; } | |
return s * int(x * p + .5) / p | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment