Author Topic: How to fill number (string) with previous 0 (zeros)  (Read 3134 times)

0 Members and 1 Guest are viewing this topic.

Offline MarcoPajotter

  • Newbie
  • Posts: 6
    • View Profile
How to fill number (string) with previous 0 (zeros)
« on: April 23, 2021, 10:35:24 am »
Hello,

And yes I have a new question,

I'm numbering a list, from 0 ... 100, and I like this line numbers as "0001" ... "0100" and not "1" ... "100"

Q) How can I fill the string with previous zero's in front of the "1" to show it as "0001"

I already did this:

Code: QB64: [Select]
  1. COMMON SHARED MyNumberValue AS LONG
  2. COMMON SHARED MyNumberString AS STRING * 8
  3.  
  4. LET MyNumberValue = 1
  5.  
  6. LET MyNumberString = "00000000"
  7. RSET MyNumberString = LTRIM$ ( RTRIM$ ( STR$ (MyNumberValue)))
  8.  
  9. PRINT MyNumberString
  10. PRINT "00000001"
  11.  


But this is not showing the good result ...

Thanks,
Marco

- every professional was once an amateur - greetings from Pajottenland - Belgium -
I love building small robots
PS: sorry for my english I speak flemish ...

FellippeHeitor

  • Guest
Re: How to fill number (string) with previous 0 (zeros)
« Reply #1 on: April 23, 2021, 10:45:55 am »
Code: QB64: [Select]
  1. Print zeropad$(1, 8)
  2.  
  3. Function zeropad$ (number&, length&)
  4.     zeropad$ = Right$(String$(length&, "0") + LTrim$(Str$(number&)), length&)

Offline zaadstra

  • Newbie
  • Posts: 78
    • View Profile
Re: How to fill number (string) with previous 0 (zeros)
« Reply #2 on: April 23, 2021, 04:02:16 pm »
Something like this?
Code: QB64: [Select]
  1. FOR i = 1 TO 100
  2.    numberstring$ = MID$(STR$(10000 + i), 3)
  3.    PRINT "'"; numberstring$; "'"
  4.  

Printed with quoutes to show there are no spaces in the strings.

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: How to fill number (string) with previous 0 (zeros)
« Reply #3 on: April 23, 2021, 09:21:32 pm »
Slight mod

DIM Digits AS INTEGER
DIM i AS _UNSIGNED LONG

Digits = 4


FOR i = 0 TO 10 ^ Digits - 1

    numberstring$ = RIGHT$(STR$(10 ^ Digits + i), Digits)

    PRINT "'"; numberstring$; "'"
NEXT i

Max digits = 6.  If more than 6 digits, the output goes exponent notation ?

« Last Edit: April 23, 2021, 09:22:57 pm by NOVARSEG »

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: How to fill number (string) with previous 0 (zeros)
« Reply #4 on: April 23, 2021, 09:34:29 pm »
This one works better.


DIM Digits AS INTEGER
DIM i AS _UNSIGNED LONG
DIM Base10 AS _UNSIGNED LONG
Digits = 7
Base10 = 10

FOR i = 0 TO 10 ^ Digits - 1

    numberstring$ = RIGHT$(STR$(Base10 ^ Digits + i), Digits)

    PRINT "'"; numberstring$; "'"
NEXT i


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How to fill number (string) with previous 0 (zeros)
« Reply #5 on: April 23, 2021, 10:03:08 pm »
Digits = 7
FOR i = 1 TO 100
    numberstring$ = RIGHT$(“0000000000000000000000000” + _TRIM$(STR$(i)), Digits)
    PRINT numberstring$
    IF i MOD 20 = 0 THEN SLEEP
NEXT i

Good for as many digits as an _INTEGER64 can contain.  Just change the value of Digits.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: How to fill number (string) with previous 0 (zeros)
« Reply #6 on: April 23, 2021, 10:42:26 pm »
With _INTEGER64  the max digits is 18?

DIM i AS _INTEGER64
DIM Digits AS INTEGER

Digits = 18

FOR i = 0 TO 10 ^ Digits - 1
    numberstring$ = RIGHT$(STRING$(Digits, "0") + _TRIM$(STR$(i)), Digits)
    PRINT numberstring$
    IF i MOD 20 = 0 THEN SLEEP
NEXT i

Hey that's a neat trick i MOD 20 = 0 !
« Last Edit: April 23, 2021, 10:44:07 pm by NOVARSEG »

Offline MarcoPajotter

  • Newbie
  • Posts: 6
    • View Profile
Re: How to fill number (string) with previous 0 (zeros)
« Reply #7 on: April 24, 2021, 02:26:35 am »
Thanks everyone,

It works well, and I need some time now to make the good choice ...

And thanks for the quick responses.
 
Marco

- every professional was once an amateur - greetings from Pajottenland - Belgium -
I love building small robots
PS: sorry for my english I speak flemish ...