Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    How to format an int as a zero padded string?

    Scheduled Pinned Locked Moved
    General Discussion
    3
    7
    252
    Loading More Posts
    • 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.
    • zaptaundefined
      zapta
      last edited by

      Let's say that I have an int variable x >= 0 which I want to format as a zero padded 7 digits string, similar to "%07d" in some programming languages. For example 127 -> "0000127". Is there a simple way to achieve? Currently I am using the brute force code below.

      if global.x < 10
        set global.y = "000000" ^ global.x
      elif global.x < 100
        set global.y = "00000" ^ global.x
      elif global.x < 1000
        set global.y = "0000" ^ global.x
      elif global.x < 10000
        set global.y = "000" ^ global.x
      elif global.x < 100000
        set global.y = "00" ^ global.x
      elif global.x < 1000000
        set global.y = "0" ^ global.x
      else
        set global.y = "" ^ global.x
        
      echo "" ^ global.y
      
      zaptaundefined 1 Reply Last reply Reply Quote 0
      • zaptaundefined
        zapta @zapta
        last edited by

        Ok, this is simpler but I wonder if there is a single statement format solution.

        var decade = 10
        var result = "" ^ global.X
        while iterations < 6
          if global.X < var.decade
            set var.result = "0" ^ var.result
          set var.decade = var.decade * 10
        
        

        Alternatively, the iteration can be on the length of the string if these is a length function (?).

        zaptaundefined 1 Reply Last reply Reply Quote 0
        • zaptaundefined
          zapta @zapta
          last edited by

          Ok, here is a simpler one. Took me some time to realize that the # unary operator require () to work.

          var pad = "" ^ global.x
          while #(var.pad) < 7
            set var.pad = "0" ^ var.pad
          
          fcwiltundefined dc42undefined 2 Replies Last reply Reply Quote 0
          • fcwiltundefined
            fcwilt @zapta
            last edited by

            @zapta

            Just out of curiosity why do you need to do this?

            Thanks.

            Frederick

            Printers: a E3D MS/TC setup and a RatRig Hybrid. Using Duet 3 hardware running 3.4.6

            zaptaundefined 1 Reply Last reply Reply Quote 0
            • zaptaundefined
              zapta @fcwilt
              last edited by

              @fcwilt, I am generating a unique id that is used to form a file name. Ideally would do it with a timestamp such as 231219-071453 but couldn't find a reliable way to generate it using gcode.

              1 Reply Last reply Reply Quote 1
              • dc42undefined
                dc42 administrators @zapta
                last edited by

                @zapta said in How to format an int as a zero padded string?:

                Took me some time to realize that the # unary operator require () to work.

                It works without the () for me, using RRF 3.5.0-rc.2.

                Duet WiFi hardware designer and firmware engineer
                Please do not ask me for Duet support via PM or email, use the forum
                http://www.escher3d.com, https://miscsolutions.wordpress.com

                zaptaundefined 1 Reply Last reply Reply Quote 1
                • zaptaundefined
                  zapta @dc42
                  last edited by

                  @dc42, must be fixed along the way. On 3.4.6 this is what i get

                  var x = "123"
                  echo "String: " ^ var.x
                  echo "Length: " ^ #var.x
                  

                  fa0fc418-7c7e-4bc9-918c-2419ea74bc77-image.png

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Unless otherwise noted, all forum content is licensed under CC-BY-SA