I'm trying to remove an integer from a decimal value that I currently have.

current syntax:

h=$(echo "scale=2; (($e/$g/$g))" | bc) 
echo $h 

The following is used to convert seconds to minutes and then hours however it returns "21.15" hours.

I want to keep the 0.15 and multiply it by 60 (leaving me with 9 minutes) -- and eventually 21 hours 9 minutes.


The bc remainder operator is %

   expr % expr
          The  result  of the expression is the "remainder" and it is com‐
          puted in the following way.  To compute a%b, first a/b  is  com‐
          puted to scale digits.  That result is used to compute a-(a/b)*b
          to the scale of the maximum of scale+scale(b) and scale(a).   If
          scale  is  set  to  zero  and both expressions are integers this
          expression is the integer remainder function.


$ echo '21.15 % 1' | bc

$ echo '(21.15 % 1) * 60' | bc

As per the bc doc:

expr / expr

The result of the expression is the quotient of the two expressions. The scale of the result is the value of the variable scale.

So use scale=0 to have an integer division:

>bc <<<'scale=2; 8/3'                                                                 

>bc <<<'scale=0; 8/3'

You can try with dc:

echo '21.15 1%60*p' | dc -f -


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.