Kenny McCormack
2023-03-17 11:20:51 UTC
Note: Both of these questions are in the "I think it is right, but am
asking to make sure" category.
I have a GAWK program that contains the following construct:
strftime("%T",($14+$15)/100,1)
which is printed out. Now, $14 and $15 are (non-negative) integer numbers
of hundredths of a second. So, you add them together and divide by 100 to
get a number of seconds, which is then printed out via strftime. I've
noticed, though, that this value is always truncated - e.g., if the sum is
299, what I get is just 2 seconds (i.e., 00:00:02). I think it would be
better if this value were rounded instead of truncated.
So, my first question is: Is there any kind of rounding function in GAWK?
(I think there is not; the only references to "round" I could find in the
man page seem to do with MPFR stuff, which I'd rather not use for this
purpose).
Second, is it enough to just add 50 before doing the division? I.e., change
it to:
strftime("%T",($14+$15+50)/100,1)
Would it be that easy?
asking to make sure" category.
I have a GAWK program that contains the following construct:
strftime("%T",($14+$15)/100,1)
which is printed out. Now, $14 and $15 are (non-negative) integer numbers
of hundredths of a second. So, you add them together and divide by 100 to
get a number of seconds, which is then printed out via strftime. I've
noticed, though, that this value is always truncated - e.g., if the sum is
299, what I get is just 2 seconds (i.e., 00:00:02). I think it would be
better if this value were rounded instead of truncated.
So, my first question is: Is there any kind of rounding function in GAWK?
(I think there is not; the only references to "round" I could find in the
man page seem to do with MPFR stuff, which I'd rather not use for this
purpose).
Second, is it enough to just add 50 before doing the division? I.e., change
it to:
strftime("%T",($14+$15+50)/100,1)
Would it be that easy?
--
"If God wanted us to believe in him, he'd exist."
(Linda Smith on "10 Funniest Londoners", TimeOut, 23rd June, 2005.)
"If God wanted us to believe in him, he'd exist."
(Linda Smith on "10 Funniest Londoners", TimeOut, 23rd June, 2005.)