• Skip to main content

Uly.me

cloud engineer

  • Home
  • About
  • Search

Leading Zeros For Numbers Below 10

August 16, 2023

When displaying in a time format, it’s best to display numbers below 10 with leading zeros.

Here’s a nice function you can call to covert numbers below 10.

convert ()
{
    # print the number as a string with a leading zero
    number=$(printf '%02d\n' "$1")
    echo $number
}
convert 5

convert () { # print the number as a string with a leading zero number=$(printf '%02d\n' "$1") echo $number } convert 5

Result

05

05

Filed Under: Linux Tagged With: calculate, leading, time, zero

  • Home
  • About
  • Search

Copyright © 2023