I'm back. After the hurricanes
I'm back. After the hurricanes
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...
I think this is what you want (number is in i%, as you might have guessed)
If you wanted Decimal places then look up PRINT USING and see if you can tailor it to your needs. =)
-abionnnn
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
-abionnnn
Like:
3 -> 3.000?
4.5 -> 4.500?
4.123456 -> 4.123
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
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#
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.
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.
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.
I'm still a bit lost
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.
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!
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!
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):3 = 003
30 = 030
300 = 300
no matter what they enter , it would be 3 digits.
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
-abionnnn
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:
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%))
Heya Neo,
If you wanted bytesize, LEN(LTRIM$(STR$(N%))) could be better written as LEN(STR$(N%))-1, thus:
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
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%))
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