From: Viper900 on 16 Jul 2010 01:44 How do i get vb to show a 0 in a variable, I want the program to read and write values like 01 and 09, but the only thing that shows in the variable is 1 or the 9. The values are being entered into text boxes and i declare variables like so var = val(text1) var1=val(text2) Then i concatenate the variables and write them to file. So that the value "09" is written I have tried the str function but that does'nt work either. Thanks
From: ralph on 16 Jul 2010 01:59 On Thu, 15 Jul 2010 22:44:04 -0700, Viper900 <Viper900(a)discussions.microsoft.com> wrote: >How do i get vb to show a 0 in a variable, I want the program to read and write >values like 01 and 09, but the only thing that shows in the variable is 1 or >the 9. > >The values are being entered into text boxes and i declare variables like so > >var = val(text1) >var1=val(text2) > >Then i concatenate the variables and write them to file. So that the value >"09" is >written >I have tried the str function but that does'nt work either. > There is no "format" associated with a number. It is simply a collection of bits that contains the value. You will either need to preserve the format by treating it like a string until you need to use its value in a computation. Or format the value to a specific format when you want to display it. -ralph
From: argusy on 16 Jul 2010 04:38 Viper900 wrote: > How do i get vb to show a 0 in a variable, I want the program to read and write > values like 01 and 09, but the only thing that shows in the variable is 1 or > the 9. > > The values are being entered into text boxes and i declare variables like so > > var = val(text1) > var1=val(text2) > > Then i concatenate the variables and write them to file. So that the value > "09" is > written > I have tried the str function but that does'nt work either. > > Thanks Adding to Ralph's reply, a number is a number, whether I add any zeros in front of it or not. That is 1, 01, 001, or 00000000000001 are all the same thing - the number "one". If you want to put zeros in front of a number, you have to convert the number to a string. say you put "001" into a textbox (text1) var = val(text1) forces var to be a number by using val() if you want var to be "001" then var = str(text1) OK, your number is now a string. write it to a file. (but make sure the datatype matches - your info is now in string format, not number format) now, let's go the other way. Let's say you've saved your variable into a file as a number, say 6. when you retrieve that number, and you want to _display_ it as "06", then you can't just add a zero in front of it. It's a number. it will be displayed as 6, not 06. What you have to do to _display_ it as "06" is to format it into a string ie, assume your number from the file is in FileVar DisplayVar = format(FileVar, "0#") The number of zeros in the second part of the format function determines the number of zeros to be displayed. To go overboard, if I wanted to display six with four zeros in front and two decimal places I'd do this DisplayVar = format(FileVar, "0000#.00") "0006.00" will be displayed, but the number is still six DisplayVar is a string, but it's _displaying_ what you want What the hell am I doing this for - any good book on VB will explain this
From: XQ135 on 16 Jul 2010 08:32 format(Var, "00")
From: Viper900 on 16 Jul 2010 21:39 Thank you, argusy Your explanation is better than any book. "argusy" wrote: > Viper900 wrote: > > How do i get vb to show a 0 in a variable, I want the program to read and write > > values like 01 and 09, but the only thing that shows in the variable is 1 or > > the 9. > > > > The values are being entered into text boxes and i declare variables like so > > > > var = val(text1) > > var1=val(text2) > > > > Then i concatenate the variables and write them to file. So that the value > > "09" is > > written > > I have tried the str function but that does'nt work either. > > > > Thanks > > Adding to Ralph's reply, a number is a number, whether I add any zeros in front > of it or not. That is 1, 01, 001, or 00000000000001 are all the same thing - the > number "one". > If you want to put zeros in front of a number, you have to convert the number to > a string. > > say you put "001" into a textbox (text1) > var = val(text1) forces var to be a number by using val() > if you want var to be "001" then > var = str(text1) > > OK, your number is now a string. write it to a file. (but make sure the datatype > matches - your info is now in string format, not number format) > > now, let's go the other way. > > Let's say you've saved your variable into a file as a number, say 6. > when you retrieve that number, and you want to _display_ it as "06", then you > can't just add a zero in front of it. It's a number. it will be displayed as 6, > not 06. > What you have to do to _display_ it as "06" is to format it into a string > ie, assume your number from the file is in FileVar > > DisplayVar = format(FileVar, "0#") > > The number of zeros in the second part of the format function determines the > number of zeros to be displayed. > > To go overboard, if I wanted to display six with four zeros in front and two > decimal places I'd do this > > DisplayVar = format(FileVar, "0000#.00") > > "0006.00" will be displayed, but the number is still six > > DisplayVar is a string, but it's _displaying_ what you want > > What the hell am I doing this for - any good book on VB will explain this > > > > > > . >
|
Pages: 1 Prev: ActiveX EXE Clues Next: Why don't my questions appear in this forum? |