2

How can I use awk to convert a time value to a decimal value.

I have been using this command for the other way round (-> from):

awk '{printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)}' <<< 1.5

prints: 1:30

how would I calculate this value 1:30 back to the decimal value 1.5?

2

How about this?

awk -F: '{printf "%.1f", ($1*60+$2)/60}' <<< 1:30
  • no, this does not work. 1:15 -> 1.2 and 1:75 -> 2.2 etc... – nath Jan 3 '19 at 22:58
  • 1
    OK. Perhaps I'm missing the point. Is 1:15 minutes not supposed to mean 1 minute and 15 seconds, and hence 1.2 minutes (rounded). If you wish different rounding (higher precision), you can change the .1 in the format string into a .2 or .3 etc. – ozzy Jan 3 '19 at 23:02
  • oh yes, I told @steeldriver ealier, I'm missing brains :-) cheers! – nath Jan 3 '19 at 23:07
  • works too, with using printf "%.2f" – nath Jan 3 '19 at 23:11

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.