help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Can Bash do simple math?


From: Greg Wooledge
Subject: Re: Can Bash do simple math?
Date: Tue, 6 Aug 2024 13:52:31 -0400

On Tue, Aug 06, 2024 at 19:07:29 +0200, bash@blaklinten.xyz wrote:
> On Tue, Aug 06, 2024 at 06:24:13PM +0200, alex xmb sw ratchev wrote:
> > i say users code has
> > seconds = diffinseconds % 60
> > not
> > minutes = diffinseconds % 60
> > 
> diff_in_seconds % 60 gives the seconds portion of diff_in_seconds.
> i.e. diff_in_seconds = 54 -> 54 % 60 = 54.
>      diff_in_seconds = 325 -> 325 % 60 = 25.

I think he simply doesn't know what the % operator does.

The % operator performs a "modulus" operation, which is just a fancy
word for "the remainder after performing an integer division".

In the second example above, 325 % 60 means "divide 325 by 60, and throw
away the quotient, but keep the remainder".  60 goes into 325 5 times
(because 60 * 5 = 300), and the remainder is 25.

Therefore, 325 % 60 = 25.

If the duration is 325 seconds, then the converted interval is 5 minutes
and 25 seconds.  In this case, when we divided 325 by 60, 5 was the
quotient, and 25 was the remainder.  For a conversion to minutes and
seconds (not hours), that's all you need to do.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]