Skip to content

Instantly share code, notes, and snippets.

@choyer
Last active November 3, 2021 02:16
Show Gist options
  • Save choyer/f239952499a4cdc772a042a1d377659e to your computer and use it in GitHub Desktop.
Save choyer/f239952499a4cdc772a042a1d377659e to your computer and use it in GitHub Desktop.
AWK/GAWK function to round float numbers to specified decimal place
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