Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How do I make a number 2 digits
Qt 6.11 is out! See what's new in the release blog

How do I make a number 2 digits

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
5 Posts 4 Posters 3.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    japer
    wrote on last edited by
    #1

    I am doing a countdown timer and I would like it in the format mm:ss. I have the code written to do the countdown and pull out a minute and second variable. The problem is when I use Text in QML and put my variable in for text: when it gets below 10 I get a single digit number. Is there anyway to get 01 instead of 1?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      A quick and simple way:

      function padValue(value) {
          if (value < 10) {
              value = "0" + i;
          }
          return value;
      }
      

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      5
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by mrjj
        #3

        Hi
        QML noob here but i was wondering if you can just write a JS function for it ?

        function pad(n) {
            return (n < 10) ? ("0" + n) : n;
        }
        

        hehe. a second too late. Yep it seems you can :)

        1 Reply Last reply
        6
        • J Offline
          J Offline
          japer
          wrote on last edited by
          #4

          Thanks everyone I ended up doing an ugly hack I am not proud of. I put another Text element that displays "0" and its only visible when my minute number is less than 10.

          JonBJ 1 Reply Last reply
          0
          • J japer

            Thanks everyone I ended up doing an ugly hack I am not proud of. I put another Text element that displays "0" and its only visible when my minute number is less than 10.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @japer
            Well don't! Change it so it's like @SGaist's or @mrjj's reply, it'll only take a second!

            1 Reply Last reply
            3

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved