Add trailing zero to awk formatted time output

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question




















  • 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






  • 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 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















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?







share|improve this question




















  • 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






  • 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 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













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?







share|improve this question












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?









share|improve this question











share|improve this question




share|improve this question










asked Dec 15 '17 at 20:48









nath

627421




627421











  • 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






  • 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 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

















  • 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






  • 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 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
















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











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





share|improve this answer






















  • 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






  • 1




    @nath I updated my answer to handle that
    – janos
    Dec 15 '17 at 23:07

















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.






share|improve this answer




















  • 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











Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















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






























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





share|improve this answer






















  • 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






  • 1




    @nath I updated my answer to handle that
    – janos
    Dec 15 '17 at 23:07














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





share|improve this answer






















  • 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






  • 1




    @nath I updated my answer to handle that
    – janos
    Dec 15 '17 at 23:07












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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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.5 would 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










  • 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




    @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












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.






share|improve this answer




















  • 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















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.






share|improve this answer




















  • 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













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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










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.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
















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













 

draft saved


draft discarded


























 


draft saved


draft discarded














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













































































Popular posts from this blog

Peggy Mitchell

Palaiologos

The Forum (Inglewood, California)