I'm back. After the hurricanes

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Bojangles

I'm back. After the hurricanes

Post by Bojangles »

I have one more question. Simple I think. How do I mask a number to 3 decimal places, whether or not it was entered a a single, doublw or triple digit number? If 1 is entered, I need 001 if 01 is entered I need 001...
abionnnn

Post by abionnnn »

I think this is what you want (number is in i%, as you might have guessed)

Code: Select all

S$ = LTRIM$(STR$(i%))
IF i% < 10 THEN
 PRINT "00"; S$
ELSEIF i% < 100 THEN
 PRINT "0"; S$
ELSE
 PRINT S$
END IF
If you wanted Decimal places then look up PRINT USING and see if you can tailor it to your needs. =)

-abionnnn
Guest

Bojangles

Post by Guest »

I'm not sure that's it. I want that if a person enters a single digit, say "3" that it converts to 003 if a person enters "45" , it converts to "045" if a person enters "266" it stays "266". I need whatever is entered to be masked to 3 decimals.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Like:
3 -> 3.000?
4.5 -> 4.500?
4.123456 -> 4.123

Code: Select all

Input "Enter num", num#
temp% = num#*1000
num# = temp%/1000
Print "3 decimal places:";num#
Theory:
Multiply by 1000, and round it, divide by 1000 to get 3 decimal points back.

If you want 2 decimal places, change 1000 to 100, for 4 decimals, 1000 should be 10000
I have left this dump.
bojangles

Post by bojangles »

Close. I know am I confusing you guys. more like

3 = 003
30 = 030
300 = 300


no matter what they enter , it would be 3 digits.
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

Post by Pete »

I think if you use a numeric variable (such as an integer) to store a number that is entered with zeroes at the beginning, they are automatically removed.

For example, if you say "x%=003" QB will make x% equal to "3". The two zeroes are removed. (You probably already figured this out...that's why you asked the question...)

So you're going to have to use string variables for this, since they allow for you to have as many zeroes as you want tacked on to the front of your numbers. You can then convert string variables to their numeric equivalents (if necessary) using VAL().

Take abionnn's code and replace "PRINT" with "LET string$ =" statements. (You'll need to manipulate it, because you'll be adding string vars together.)

Then to use your string variable values as numbers, you just convert them with the VAL() function. If string$="12345", VAL(string$) = the number 12345.
Bojangles

I'm still a bit lost

Post by Bojangles »

In my native language we have masking. I can have a 1 decimal value say "3" for instance and them add an R%3 after it and it becomes "003". I was hoping qbasic had a similar function. Here's a brief summary of why I need this. The day of year is can be anywhere from 1 to 3 digits in length , depending on how far into the year we are. This number is part of a larger number, but I need the space it occupies in the larger number to be consistent. say for instance this is the larger number 42661211. The 266 is the day of year. If the day of year was only the 9th day or January 9. the number needs to read 40091211. I just need to figure out how to convert value 9 to be 009 so the position is fixed.
Bojangles

Post by Bojangles »

Hmmm Perhaps if I follow your suggestion and simply do a Len on the dayof year and if the len is < 3 then prefix the necessary amount of Zeros to fill it out. I'll try that. I don't care if it's a patchy solution as long as it works. Thanks.. ;)
bojangles

Post by bojangles »

Ok I give up. How do I prefix "0" zeros to the getdayofyear% ?

I tried it ltrim$(str$(getdayofyear%)) = 2 then ltrim$(str$(getdayofyear%)) = "0" + ltrim$(str$(getdayofyear%))
end


it doesn't like it. Keep in mind qbasic isn't the language I code in so I am struggling. I am leaning on you pro's to throw me a bone here! ;)
Bojangles

Post by Bojangles »

I got it. Thanks for your help. :D
Guest

Post by Guest »

3 = 003
30 = 030
300 = 300

no matter what they enter , it would be 3 digits.
The snippet I gave you should do it. Give it a try... if you want more digits, say n% then you can figure out how many digits i% has by this formula (make sure i% <> 0):

d% = INT(1 + LOG(i%)/LOG(10))

Then your output string is simply:

S$ = STRING$(n%-d%, &H30) + LTRIM$(STR$(i%))

Putting it together:

Code: Select all

'PAD$ function for Bojangles!
'input: n% is the number of digits to display, m% is the number to display
FUNCTION PAD$ (n%, m%)
 IF m% <> 0 THEN
  PAD$ = STRING$(n% - INT(1 + LOG(m%) / LOG(10)), &H30) + LTRIM$(STR$(m%))
 ELSE
  PAD$ = STRING$(n%, &H30)
 END IF
END FUNCTION
Try it out like so ?PAD$(3, 3), ?PAD$(3,30), etc.

-abionnnn
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

Anonymous wrote:-abionnnn
abionnnn?! ??? man, WHERE HAVE YOU BEEN :D
Anonymous

Post by Anonymous »

Why does everyone give such a big block of code?

In the following code, N% is your number, and PLACES% the amount of zeros you want to have.

Here it is:

Code: Select all

N% = 30
PLACES% = 3
PRINT STRING$(PLACES% - LEN(LTRIM$(STR$(N%))), "0") + LTRIM$(STR$(N%))
Guest

Bojangles

Post by Guest »

I wish I'd have seen that yesterday. I already put the code into the production program. I did it the hard way and did an If then on the len of the variable. I prefixed Zero's depending on the len of the variable. See, I told you I'm no expert. :oops:
abionnnn

Post by abionnnn »

Heya Neo,

If you wanted bytesize, LEN(LTRIM$(STR$(N%))) could be better written as LEN(STR$(N%))-1, thus:

Code: Select all

N% = 30
P% = 3 
?STRING$(p%-LEN(STR(n%))+1,"0")+LTRIM$(STR$(n%))
Geez, why do people write such big blocks of code? Hehehe =P (Just kidding)

Nekrophidus! It HAS been a long time hasn't it my friend?! I'm so glad to see you're active in the scene. Where the heck have I been? Well that's a really long story, much too long not to discuss in real time. Do you still use IRC/ICQ? (my icq#is still 11969960)

-abionnnn
Nodtveidt
Veteran
Posts: 826
Joined: Sun Jul 25, 2004 4:24 am
Location: Quebradillas, PR
Contact:

Post by Nodtveidt »

11541913 here. I just reinstalled ICQ, so perhaps I'll catch you online sometime. And yeah, I'm active in the scene again...barok kinda convinced me to come back last October (after a 2 year hiatus) and I've been paddling around since then. :D
Post Reply