Menu

How to print leading zeros on LCD?

2024-10-23
2024-10-24
  • Steve Scott

    Steve Scott - 2024-10-23

    Hello All-
    This is a simple question but I cannot figure out how to do this.
    I have a byte variable and I want to display its value on the LCD.
    My issue is when the value goes from '10' to '9', the display shows '90'.
    How can I get it to show '09'?
    Simple, but can not see it.......

     
  • Steve Scott

    Steve Scott - 2024-10-24

    Thomas, thanks for the info, however, not exactly what I am wanting. I did try what you suggested:
    locate 3,5 : print leftpad(minutes,2)
    locate 3,12 :print leftpad(seconds,2)
    and it gave me strange symbols and not a 09.
    Let me try be more clear.
    In another compiler (PBP) I could use DEC(number of digits) and it would automatically show
    11 then 10, then 09, then 08 as it was counting down.
    Similarly, I could also use HEX(number of digits) and it would do the same for me.
    I am wanting to display a minutes timer display which counts down to zero, then zeros out and the seconds timer display counts down from 60 to zero using leading zeros.
    Does this make it any clearer?
    I have spent time looking for other demos or code fragments for GCB but can not find any - I am certain I am not asking the correct question.

     
  • Kato

    Kato - 2024-10-24

    Your syntax is wrong.

    Print leftpad( str(Fract),4,"0") implies Fract is an Integer.

    So is minutes an Int? try print leftpad(str(minutes),2) to convert the

    But, to use a lot less resources, try something simple.

    ~~~
    locate 3,5
    if minutes < 10 then
    print "0"
    print Minutes
    else
    print Minutes
    end if
    ~~~

    This program does not call the leftpad() function as that is resource hungry, or, str()

     
  • Steve Scott

    Steve Scott - 2024-10-24

    Kato,
    Your second approach works great, I wish GCB had something to this is a single command, such as DEC(2) minutes!

     
  • Kato

    Kato - 2024-10-24

    You can create a function called DEC()

    print StringToDEC( "1" )
    
    // ----- Main body of program commences here.
    // handle string
    function StringToDEC( indecstring as String ) as String
        if indecstring(0) = 1 then
                StringToDEC = "0" + indecstring
         else 
                 StringToDEC = indecstring
        end if
    end function
    
    // handle integer
    function IntegerToDEC ( in indecinteger as integer ) as String
        if indecinteger < 10 then
                IntegerToDEC = "0" + str(indecinteger)
         else 
                 IntegerToDEC = str(indecinteger)
        end if
    end function
    
     
  • Steve Scott

    Steve Scott - 2024-10-24

    Thanks! Never used one, but I will try it out.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.