|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Newbie help on String conversion needed pleaseSorry for the newbie question, however this might be an easy one.
I have the following senerio SFee = Val(Fees.Text) Where in a text box called Fees.Text I have a string value of "1,000.00" I have the following: Dim SFee as Long When I run this, SFee comes out to 1 instead of 1000. Any idea what I am doing wrong? Thanks in Advance LS Hi
The problem is due to the thousands seperator, as far as Val is concerned the number ends at the comma. Try SFee - Val(replace(fees.text, ",", "")) If this program is going to be used in other countries you should get the regional seperator character and use that instead of the comma above. Show quote "Lorne Steiner" <lorne_stei***@hotmail.com> wrote in message news:%23h0%23kkwVFHA.3716@TK2MSFTNGP12.phx.gbl... > Sorry for the newbie question, however this might be an easy one. > > I have the following senerio > SFee = Val(Fees.Text) > Where in a text box called Fees.Text I have a string value of "1,000.00" > I have the following: > Dim SFee as Long > > When I run this, SFee comes out to 1 instead of 1000. Any idea what I am > doing wrong? > > Thanks in Advance > LS > Per MSDN:
The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. Show quote "Lorne Steiner" <lorne_stei***@hotmail.com> wrote in message news:%23h0%23kkwVFHA.3716@TK2MSFTNGP12.phx.gbl... > Sorry for the newbie question, however this might be an easy one. > > I have the following senerio > SFee = Val(Fees.Text) > Where in a text box called Fees.Text I have a string value of "1,000.00" > I have the following: > Dim SFee as Long > > When I run this, SFee comes out to 1 instead of 1000. Any idea what I am > doing wrong? > > Thanks in Advance > LS > > Try this ...
Private Sub Command1_Click() Dim d As Double 'nothing to do if empty If Len(Text1.Text) > 0 Then 'if val works, then If Val(Text1.Text) > 0 Then 'cdbl should as well d = (CDbl(Text1.Text)) Print d, TypeName(d) End If End If End Sub -- Show quoteRandy Birch MS MVP Visual Basic http://vbnet.mvps.org/ ---------------------------------------------------------------------------- Read. Decide. Sign the petition to Microsoft. http://classicvb.org/petition/ ---------------------------------------------------------------------------- "Lorne Steiner" <lorne_stei***@hotmail.com> wrote in message news:%23h0%23kkwVFHA.3716@TK2MSFTNGP12.phx.gbl... : Sorry for the newbie question, however this might be an easy one. : : I have the following senerio : SFee = Val(Fees.Text) : Where in a text box called Fees.Text I have a string value of "1,000.00" : I have the following: : Dim SFee as Long : : When I run this, SFee comes out to 1 instead of 1000. Any idea what I am : doing wrong? : : Thanks in Advance : LS : : As Dave suggested, Val() is not a good idea if you plan to run your code in
other locales, i.e. other countries. Something like CDbl() will also convert text to number, but will honour both the 'triple separator' and the decimal separator used in the current locale. Hence, it's ideal for what you want. Val(), on the other hand, only accepts the '.' decimal separator, and does not accept 'triple separators' at all. Some people say that Val() (and also Str()) is therefore not locale-aware, but a more useful interpretation is that it honours the programming locale rather than the user's regional locale. Str(), for instance, can be used to reliably format a number for a SQL or MDX query, but CStr cannot. There are some quirks though. Both Val() and CDbl() accept E-format numbers (e.g. 1.23E+3), which can be really annoying if you only want to accept simple decimals. Also, as you've already found, Val() doesn't report any errors. It simply stops reading when it finds a character it can't convert. Tony Proctor Show quote "Lorne Steiner" <lorne_stei***@hotmail.com> wrote in message news:#h0#kkwVFHA.3716@TK2MSFTNGP12.phx.gbl... > Sorry for the newbie question, however this might be an easy one. > > I have the following senerio > SFee = Val(Fees.Text) > Where in a text box called Fees.Text I have a string value of "1,000.00" > I have the following: > Dim SFee as Long > > When I run this, SFee comes out to 1 instead of 1000. Any idea what I am > doing wrong? > > Thanks in Advance > LS > > On Fri, 13 May 2005 19:33:30 +0100, "Tony Proctor"
<tony_proctor@aimtechnology_NoMoreSPAM_.com> wrote: >As Dave suggested, Val() is not a good idea if you plan to run your code in Eh ?>other locales, i.e. other countries. Something like CDbl() will also convert >text to number, but will honour both the 'triple separator' and the decimal >separator used in the current locale. Hence, it's ideal for what you want. > >Val(), on the other hand, only accepts the '.' decimal separator, and does >not accept 'triple separators' at all. Some people say that Val() (and also >Str()) is therefore not locale-aware, but a more useful interpretation is >that it honours the programming locale rather than the user's regional >locale. Surely that is slightly misleading Val() and Str() always use '.' as the decimal separator - regardless of where one is programming The other thing is that Val() is very tolerant of dirty data - like the stuff users input - whereas CDbl() will blow on anything non numeric A programming language has it's own conceptual locale, and this controls the
format of decimal numbers (VB always uses '.'), the format of date constants (VB uses #US-format#), the format of boolean constants (always "True"/"False"), and the character set (Unicode for Unicode). This is independent of the user's own regional locale since languages have to be portable. The same applies to other 'programming interfaces', e.g. SQL, MDX, XML, etc. The Posix standard for national language support even defines a special 'C locale' that can be selected. The point I was trying to make is that rather than Val/Str simply being anachronisms from early VB, they can be viewed as a way of manipulating decimal numbers in the programming locale. The example I gave of generating a SQL query with a floating point value in it illustrates it quite nicely. If you format the number using CStr() then the query will fail in regional locales that use a ',' decimal separator. Using Str() (however naff the function is) at least guarantees that the representation matches the SQL programming locale. We both know the pro's and con's of the different interfaces. I'm merely advocating a different interpretation of their differences Tony Proctor Show quote "J French" <erew***@nowhere.uk> wrote in message news:428524c9.66397382@news.btclick.com... > On Fri, 13 May 2005 19:33:30 +0100, "Tony Proctor" > <tony_proctor@aimtechnology_NoMoreSPAM_.com> wrote: > > >As Dave suggested, Val() is not a good idea if you plan to run your code in > >other locales, i.e. other countries. Something like CDbl() will also convert > >text to number, but will honour both the 'triple separator' and the decimal > >separator used in the current locale. Hence, it's ideal for what you want. > > > >Val(), on the other hand, only accepts the '.' decimal separator, and does > >not accept 'triple separators' at all. Some people say that Val() (and also > >Str()) is therefore not locale-aware, but a more useful interpretation is > >that it honours the programming locale rather than the user's regional > >locale. > > Eh ? > Surely that is slightly misleading > Val() and Str() always use '.' as the decimal separator > - regardless of where one is programming > > The other thing is that Val() is very tolerant of dirty data > - like the stuff users input > - whereas CDbl() will blow on anything non numeric > > On Sat, 14 May 2005 11:20:39 +0100, "Tony Proctor"
<tony_proctor@aimtechnology_NoMoreSPAM_.com> wrote: Show quote >A programming language has it's own conceptual locale, and this controls the I am advocating an understanding of :>format of decimal numbers (VB always uses '.'), the format of date constants >(VB uses #US-format#), the format of boolean constants (always >"True"/"False"), and the character set (Unicode for Unicode). This is >independent of the user's own regional locale since languages have to be >portable. > >The same applies to other 'programming interfaces', e.g. SQL, MDX, XML, etc. >The Posix standard for national language support even defines a special 'C >locale' that can be selected. The point I was trying to make is that rather >than Val/Str simply being anachronisms from early VB, they can be viewed as >a way of manipulating decimal numbers in the programming locale. The example >I gave of generating a SQL query with a floating point value in it >illustrates it quite nicely. If you format the number using CStr() then the >query will fail in regional locales that use a ',' decimal separator. Using >Str() (however naff the function is) at least guarantees that the >representation matches the SQL programming locale. > >We both know the pro's and con's of the different interfaces. I'm merely >advocating a different interpretation of their differences Date$, Time$, Val() and Str$() There is a lot be to said for knowing what one is doing Just happens that I believe that localization should be up to the programmer. |
|||||||||||||||||||||||