Add trailing zero to awk formatted time output

Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I use the following command to convert a decimal value into a time value:
echo "1.5" | awk -F'.' 'printf $1 ":" "%.0f", $2 / 100 * 60'
outputs: 1:3
how can I make awk to add a trailing zero to the output so I would get: 1:30?
awk
add a comment |Â
up vote
2
down vote
favorite
I use the following command to convert a decimal value into a time value:
echo "1.5" | awk -F'.' 'printf $1 ":" "%.0f", $2 / 100 * 60'
outputs: 1:3
how can I make awk to add a trailing zero to the output so I would get: 1:30?
awk
What if you had a number like24.1(a number > 24)? Then what you would expect as result?
â Ã±ÃÂsýù÷
Dec 15 '17 at 21:15
@ñÃÂsýù÷24.1 = 24:06in my case I'm asking aboutHH:MMso I would have24 hours and 6 minutesbut I see that I should edit my question to make clearer what I was taking about it is not mentioned anywhere. Thanks for pointing that out!
â nath
Dec 15 '17 at 21:21
1
Another thought:hours=1.5; date -u -d "@$(echo "$hours * 60 * 60" | bc )" "+%H:%M"
â glenn jackman
Dec 15 '17 at 21:47
@glennjackman you guys are throwing at me with code :-) I don't get to edit my question, by being busy just reading and understanding the answers... THANKS! thedateone is also a pretty nice one. Thought to just polish the output of my script and now get a complete command-line/bash tutorial :-)
â nath
Dec 15 '17 at 21:52
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I use the following command to convert a decimal value into a time value:
echo "1.5" | awk -F'.' 'printf $1 ":" "%.0f", $2 / 100 * 60'
outputs: 1:3
how can I make awk to add a trailing zero to the output so I would get: 1:30?
awk
I use the following command to convert a decimal value into a time value:
echo "1.5" | awk -F'.' 'printf $1 ":" "%.0f", $2 / 100 * 60'
outputs: 1:3
how can I make awk to add a trailing zero to the output so I would get: 1:30?
awk
asked Dec 15 '17 at 20:48
nath
627421
627421
What if you had a number like24.1(a number > 24)? Then what you would expect as result?
â Ã±ÃÂsýù÷
Dec 15 '17 at 21:15
@ñÃÂsýù÷24.1 = 24:06in my case I'm asking aboutHH:MMso I would have24 hours and 6 minutesbut I see that I should edit my question to make clearer what I was taking about it is not mentioned anywhere. Thanks for pointing that out!
â nath
Dec 15 '17 at 21:21
1
Another thought:hours=1.5; date -u -d "@$(echo "$hours * 60 * 60" | bc )" "+%H:%M"
â glenn jackman
Dec 15 '17 at 21:47
@glennjackman you guys are throwing at me with code :-) I don't get to edit my question, by being busy just reading and understanding the answers... THANKS! thedateone is also a pretty nice one. Thought to just polish the output of my script and now get a complete command-line/bash tutorial :-)
â nath
Dec 15 '17 at 21:52
add a comment |Â
What if you had a number like24.1(a number > 24)? Then what you would expect as result?
â Ã±ÃÂsýù÷
Dec 15 '17 at 21:15
@ñÃÂsýù÷24.1 = 24:06in my case I'm asking aboutHH:MMso I would have24 hours and 6 minutesbut I see that I should edit my question to make clearer what I was taking about it is not mentioned anywhere. Thanks for pointing that out!
â nath
Dec 15 '17 at 21:21
1
Another thought:hours=1.5; date -u -d "@$(echo "$hours * 60 * 60" | bc )" "+%H:%M"
â glenn jackman
Dec 15 '17 at 21:47
@glennjackman you guys are throwing at me with code :-) I don't get to edit my question, by being busy just reading and understanding the answers... THANKS! thedateone is also a pretty nice one. Thought to just polish the output of my script and now get a complete command-line/bash tutorial :-)
â nath
Dec 15 '17 at 21:52
What if you had a number like
24.1 (a number > 24)? Then what you would expect as result?â Ã±ÃÂsýù÷
Dec 15 '17 at 21:15
What if you had a number like
24.1 (a number > 24)? Then what you would expect as result?â Ã±ÃÂsýù÷
Dec 15 '17 at 21:15
@ñÃÂsýù÷
24.1 = 24:06 in my case I'm asking about HH:MM so I would have 24 hours and 6 minutes but I see that I should edit my question to make clearer what I was taking about it is not mentioned anywhere. Thanks for pointing that out!â nath
Dec 15 '17 at 21:21
@ñÃÂsýù÷
24.1 = 24:06 in my case I'm asking about HH:MM so I would have 24 hours and 6 minutes but I see that I should edit my question to make clearer what I was taking about it is not mentioned anywhere. Thanks for pointing that out!â nath
Dec 15 '17 at 21:21
1
1
Another thought:
hours=1.5; date -u -d "@$(echo "$hours * 60 * 60" | bc )" "+%H:%M"â glenn jackman
Dec 15 '17 at 21:47
Another thought:
hours=1.5; date -u -d "@$(echo "$hours * 60 * 60" | bc )" "+%H:%M"â glenn jackman
Dec 15 '17 at 21:47
@glennjackman you guys are throwing at me with code :-) I don't get to edit my question, by being busy just reading and understanding the answers... THANKS! the
date one is also a pretty nice one. Thought to just polish the output of my script and now get a complete command-line/bash tutorial :-)â nath
Dec 15 '17 at 21:52
@glennjackman you guys are throwing at me with code :-) I don't get to edit my question, by being busy just reading and understanding the answers... THANKS! the
date one is also a pretty nice one. Thought to just polish the output of my script and now get a complete command-line/bash tutorial :-)â nath
Dec 15 '17 at 21:52
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
I don't think that adding a trailing 0 is a good approach.
It may produce the intended output for the 1.5 as input,
but if you need a general solution for other inputs,
this approach will probably not work well.
A better approach is to not split the integer part and the decimal parts,
but to work with minutes, using the / and % operators to compute the correct hours and minutes, for example:
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.5
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.50
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.7
# prints 1:42
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.05
# prints 1:03
To handle negative values,
you can introduce an abs function:
awk 'function abs(v) return v < 0 ? -v : v
printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< -1.7
# prints -1:42
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
the only problem I experienced so far is that it does not work with minus values.-1.5would result in-1:-30
â nath
Dec 15 '17 at 22:54
1
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
add a comment |Â
up vote
1
down vote
Since you don't know how many fractional digits you have, you don't know that you need to divide by 100. @janos has the correct answer, but if you insist on splitting on the decimal point, you need to force convert the fractional part back into a fractional number:
echo 1.5 | awk -F'.' 'printf "%d:%02dn", $1, ("0."$2) * 60'
"0."$2 is string concatenation resulting in the string "0.5".
Then, multiply by the number 60 to get the number of minutes.
Or, you can figure out what divisor to use, based on the number of digits
awk -F'.' 'printf "%st%d:%02dn", $0, $1, $2 / 10**length($2) * 60' << END
1
1.5
1.75
1.6666667
END
1 1:00
1.5 1:30
1.75 1:45
1.6666667 1:40
But, don't do either of these things.
the advantage of this command so far seems to be the fact that it works with minus values:-1.5results in-1:30
â nath
Dec 15 '17 at 22:57
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
I don't think that adding a trailing 0 is a good approach.
It may produce the intended output for the 1.5 as input,
but if you need a general solution for other inputs,
this approach will probably not work well.
A better approach is to not split the integer part and the decimal parts,
but to work with minutes, using the / and % operators to compute the correct hours and minutes, for example:
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.5
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.50
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.7
# prints 1:42
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.05
# prints 1:03
To handle negative values,
you can introduce an abs function:
awk 'function abs(v) return v < 0 ? -v : v
printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< -1.7
# prints -1:42
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
the only problem I experienced so far is that it does not work with minus values.-1.5would result in-1:-30
â nath
Dec 15 '17 at 22:54
1
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
add a comment |Â
up vote
5
down vote
accepted
I don't think that adding a trailing 0 is a good approach.
It may produce the intended output for the 1.5 as input,
but if you need a general solution for other inputs,
this approach will probably not work well.
A better approach is to not split the integer part and the decimal parts,
but to work with minutes, using the / and % operators to compute the correct hours and minutes, for example:
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.5
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.50
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.7
# prints 1:42
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.05
# prints 1:03
To handle negative values,
you can introduce an abs function:
awk 'function abs(v) return v < 0 ? -v : v
printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< -1.7
# prints -1:42
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
the only problem I experienced so far is that it does not work with minus values.-1.5would result in-1:-30
â nath
Dec 15 '17 at 22:54
1
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
I don't think that adding a trailing 0 is a good approach.
It may produce the intended output for the 1.5 as input,
but if you need a general solution for other inputs,
this approach will probably not work well.
A better approach is to not split the integer part and the decimal parts,
but to work with minutes, using the / and % operators to compute the correct hours and minutes, for example:
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.5
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.50
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.7
# prints 1:42
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.05
# prints 1:03
To handle negative values,
you can introduce an abs function:
awk 'function abs(v) return v < 0 ? -v : v
printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< -1.7
# prints -1:42
I don't think that adding a trailing 0 is a good approach.
It may produce the intended output for the 1.5 as input,
but if you need a general solution for other inputs,
this approach will probably not work well.
A better approach is to not split the integer part and the decimal parts,
but to work with minutes, using the / and % operators to compute the correct hours and minutes, for example:
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.5
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.50
# prints 1:30
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.7
# prints 1:42
awk 'printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< 1.05
# prints 1:03
To handle negative values,
you can introduce an abs function:
awk 'function abs(v) return v < 0 ? -v : v
printf "%d:%02d", ($1 * 60 / 60), ($1 * 60 % 60)' <<< -1.7
# prints -1:42
edited Dec 15 '17 at 23:07
answered Dec 15 '17 at 20:59
janos
6,81322247
6,81322247
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
the only problem I experienced so far is that it does not work with minus values.-1.5would result in-1:-30
â nath
Dec 15 '17 at 22:54
1
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
add a comment |Â
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
the only problem I experienced so far is that it does not work with minus values.-1.5would result in-1:-30
â nath
Dec 15 '17 at 22:54
1
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
thanks, this is exactly what I have been looking for. I only use the command for the value being printed to the terminal to be human readable. All calculation is done before with the decimal value.
â nath
Dec 15 '17 at 21:08
the only problem I experienced so far is that it does not work with minus values.
-1.5 would result in -1:-30â nath
Dec 15 '17 at 22:54
the only problem I experienced so far is that it does not work with minus values.
-1.5 would result in -1:-30â nath
Dec 15 '17 at 22:54
1
1
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
@nath I updated my answer to handle that
â janos
Dec 15 '17 at 23:07
add a comment |Â
up vote
1
down vote
Since you don't know how many fractional digits you have, you don't know that you need to divide by 100. @janos has the correct answer, but if you insist on splitting on the decimal point, you need to force convert the fractional part back into a fractional number:
echo 1.5 | awk -F'.' 'printf "%d:%02dn", $1, ("0."$2) * 60'
"0."$2 is string concatenation resulting in the string "0.5".
Then, multiply by the number 60 to get the number of minutes.
Or, you can figure out what divisor to use, based on the number of digits
awk -F'.' 'printf "%st%d:%02dn", $0, $1, $2 / 10**length($2) * 60' << END
1
1.5
1.75
1.6666667
END
1 1:00
1.5 1:30
1.75 1:45
1.6666667 1:40
But, don't do either of these things.
the advantage of this command so far seems to be the fact that it works with minus values:-1.5results in-1:30
â nath
Dec 15 '17 at 22:57
add a comment |Â
up vote
1
down vote
Since you don't know how many fractional digits you have, you don't know that you need to divide by 100. @janos has the correct answer, but if you insist on splitting on the decimal point, you need to force convert the fractional part back into a fractional number:
echo 1.5 | awk -F'.' 'printf "%d:%02dn", $1, ("0."$2) * 60'
"0."$2 is string concatenation resulting in the string "0.5".
Then, multiply by the number 60 to get the number of minutes.
Or, you can figure out what divisor to use, based on the number of digits
awk -F'.' 'printf "%st%d:%02dn", $0, $1, $2 / 10**length($2) * 60' << END
1
1.5
1.75
1.6666667
END
1 1:00
1.5 1:30
1.75 1:45
1.6666667 1:40
But, don't do either of these things.
the advantage of this command so far seems to be the fact that it works with minus values:-1.5results in-1:30
â nath
Dec 15 '17 at 22:57
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Since you don't know how many fractional digits you have, you don't know that you need to divide by 100. @janos has the correct answer, but if you insist on splitting on the decimal point, you need to force convert the fractional part back into a fractional number:
echo 1.5 | awk -F'.' 'printf "%d:%02dn", $1, ("0."$2) * 60'
"0."$2 is string concatenation resulting in the string "0.5".
Then, multiply by the number 60 to get the number of minutes.
Or, you can figure out what divisor to use, based on the number of digits
awk -F'.' 'printf "%st%d:%02dn", $0, $1, $2 / 10**length($2) * 60' << END
1
1.5
1.75
1.6666667
END
1 1:00
1.5 1:30
1.75 1:45
1.6666667 1:40
But, don't do either of these things.
Since you don't know how many fractional digits you have, you don't know that you need to divide by 100. @janos has the correct answer, but if you insist on splitting on the decimal point, you need to force convert the fractional part back into a fractional number:
echo 1.5 | awk -F'.' 'printf "%d:%02dn", $1, ("0."$2) * 60'
"0."$2 is string concatenation resulting in the string "0.5".
Then, multiply by the number 60 to get the number of minutes.
Or, you can figure out what divisor to use, based on the number of digits
awk -F'.' 'printf "%st%d:%02dn", $0, $1, $2 / 10**length($2) * 60' << END
1
1.5
1.75
1.6666667
END
1 1:00
1.5 1:30
1.75 1:45
1.6666667 1:40
But, don't do either of these things.
answered Dec 15 '17 at 21:39
glenn jackman
46.7k265103
46.7k265103
the advantage of this command so far seems to be the fact that it works with minus values:-1.5results in-1:30
â nath
Dec 15 '17 at 22:57
add a comment |Â
the advantage of this command so far seems to be the fact that it works with minus values:-1.5results in-1:30
â nath
Dec 15 '17 at 22:57
the advantage of this command so far seems to be the fact that it works with minus values:
-1.5 results in -1:30â nath
Dec 15 '17 at 22:57
the advantage of this command so far seems to be the fact that it works with minus values:
-1.5 results in -1:30â nath
Dec 15 '17 at 22:57
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f411138%2fadd-trailing-zero-to-awk-formatted-time-output%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
What if you had a number like
24.1(a number > 24)? Then what you would expect as result?â Ã±ÃÂsýù÷
Dec 15 '17 at 21:15
@ñÃÂsýù÷
24.1 = 24:06in my case I'm asking aboutHH:MMso I would have24 hours and 6 minutesbut I see that I should edit my question to make clearer what I was taking about it is not mentioned anywhere. Thanks for pointing that out!â nath
Dec 15 '17 at 21:21
1
Another thought:
hours=1.5; date -u -d "@$(echo "$hours * 60 * 60" | bc )" "+%H:%M"â glenn jackman
Dec 15 '17 at 21:47
@glennjackman you guys are throwing at me with code :-) I don't get to edit my question, by being busy just reading and understanding the answers... THANKS! the
dateone is also a pretty nice one. Thought to just polish the output of my script and now get a complete command-line/bash tutorial :-)â nath
Dec 15 '17 at 21:52