Qbasicnews.com
Number Formatting - Printable Version

+- Qbasicnews.com (http://qbasicnews.com/newforum)
+-- Forum: Qbasic "like" compilers/interpreters (http://qbasicnews.com/newforum/forum-5.html)
+--- Forum: FB Discussion & Programming Help (http://qbasicnews.com/newforum/forum-15.html)
+--- Thread: Number Formatting (/thread-7655.html)

Pages: 1 2


Number Formatting - Rokkuman - 07-04-2005

Is there any specific way to display all numbers in a "000" format? I tried the USING command, but it doesn't seem to have that. What I'm trying to do is load files that are sorted as "File001.dat, File002.dat, File003.dat .......... File010.dat, File011.dat".


Number Formatting - MystikShadows - 07-04-2005

well I thought that USING "000" would force the leading zeros to appear. Hmmm

if not you can always do something like:

Code:
IF NumericValue < 10 THEN
   LeadingZeroes = "00"
ELSE IF NumericValue > 9 AND NumericValue <=99 THEN
   LeadingZeroes = "0"
ELSE
   LeadingZeroes = ""
END IF

FileName = "FILE" + LeadingZeroes + TRIM$(STR$(NumericValue))+".dat"

Should do the trick :-)


Number Formatting - Rokkuman - 07-04-2005

Alright, I'll go with that. Thanks. =]


Number Formatting - MystikShadows - 07-04-2005

You're very welcome :-)


Number Formatting - Moneo - 07-04-2005

Rokkuman,

Here's a solution based on the same idea, but much simpler:
Code:
FileName$ = "FILE" + RIGHT$("000" + LTRIM$(STR$(NumericValue)) ,3)+".dat"
Note: You really only need 2 zeros up front, "00",
but using "000" makes what you're doing clearer.
*****


Number Formatting - MystikShadows - 07-05-2005

I thought doing a STR$(Numeric) left a leading space for the possible minus (-) sign)


Number Formatting - rpgfan3233 - 07-05-2005

Not in FB. In QB, it left a leading space for the minus sign, but in FB, that is changed. This makes things easier so you don't have to do this:
Code:
RIGHT$(STR$(variable), LEN(STR$(variable)) - 1)

This removes the first character and should only be used in QB if you know that a result will be positive. Otherwise, you would probably use a FUNCTION or a SUB to convert it to a string if you convert to strings numerous times:

Code:
FUNCTION varToStr$(variable)
IF SGN(variable) < 0 THEN
    varToStr$ = STR$(variable)
ELSE
    varToStr$ = RIGHT$(STR$(variable), LEN(STR$(variable)) - 1)
END IF
END FUNCTION

Edit: Untested, but it should work.


Number Formatting - dumbledore - 07-05-2005

O_o in qb people used ltrim$( str$( somenum% ) ) :roll: :wink:


Number Formatting - rpgfan3233 - 07-05-2005

Quote:O_o in qb people used ltrim$( str$( somenum% ) ) :roll: :wink:
Except for people who didn't know that LTRIM$ existed. :wink:


Number Formatting - Neo - 07-05-2005

Another way to do it is:

Code:
Const NumberOfZeros = 3


FileName$ = "file" + String$(NumberOfZeros - Len(LTrim$(Str$(NumericalValue))), "0") + LTrim$(Str$(NumericalValue))

Easily adjust number of preceding zeros Smile