| Line | |
|---|
| 1 | # round.awk --- do normal rounding
|
|---|
| 2 | #
|
|---|
| 3 | # Arnold Robbins, [email protected], Public Domain
|
|---|
| 4 | # August, 1996
|
|---|
| 5 |
|
|---|
| 6 | function round(x, ival, aval, fraction)
|
|---|
| 7 | {
|
|---|
| 8 | ival = int(x) # integer part, int() truncates
|
|---|
| 9 |
|
|---|
| 10 | # see if fractional part
|
|---|
| 11 | if (ival == x) # no fraction
|
|---|
| 12 | return x
|
|---|
| 13 |
|
|---|
| 14 | if (x < 0) {
|
|---|
| 15 | aval = -x # absolute value
|
|---|
| 16 | ival = int(aval)
|
|---|
| 17 | fraction = aval - ival
|
|---|
| 18 | if (fraction >= .5)
|
|---|
| 19 | return int(x) - 1 # -2.5 --> -3
|
|---|
| 20 | else
|
|---|
| 21 | return int(x) # -2.3 --> -2
|
|---|
| 22 | } else {
|
|---|
| 23 | fraction = x - ival
|
|---|
| 24 | if (fraction >= .5)
|
|---|
| 25 | return ival + 1
|
|---|
| 26 | else
|
|---|
| 27 | return ival
|
|---|
| 28 | }
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | # test harness
|
|---|
| 32 | { print $0, round($0) }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.