Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string to val again

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
If I have strings like '1,234,567.88' is there a fn that
convert it to numeric. Val() won't work, it will give
you 1 in this case (val of anything before the first
comma) Tks
 
First strip out the commas - you need to leave the periods

To do this use the strtran function

STRTRAN(cSearched, cSearchFor [, cReplacement]
[, nStartOccurrence] [, nNumberOfOccurrences])

in your example...

myString = '1,234,567.88'
myNumber = Val(StrTran(myString,",",""))
myInteger = Int(myNumber)

HTH
Griff
[smile]
 
if the string contains more than 10 digitals.
it will return in like that:
1234567890 -> 1.234E + 10

can I still change to "1234567890" ?
 
tony_chan

I can not follow your last question.
I tried it with the following number
by using griff's code.

myString = '10,213,567,333.88'
and the result was ok, as mynumber was shown as 10213567333.88

As the word digitals has a lot of meanings in german - could you give an example of
the string with more than 10 digitals so that I can try to test it here?
Maybe that then an answer can be found. I am interested myself in the reason of
your wrong result.


Regards from germany (time = 13.02 h here)
Klaus

 
Never had to do that, I guess I would test for the E +,
then multiply up - but by the time you are into that kind of notation - you can only get an approximation of the number i.e. 1.234 times 10 to the power of 10 is going to loose some of the least significant digits (567890) thats the disadvantage of the format however...

To test:

myString = "1.234E + 10"

Function Scientific
parameter myString
Private myString,myNumber,mySignificant,Multi
myString = Upper(myString)
** first remove all spaces
myString = StrTran(myString," ","") && now its 1.234E+10
** next remove the signifant bit to the left of the E
mySignificant = Val(Left(myString,AT("E",myString)-1))
** should now have 1.234 in it
** find and work out the multiplier
Multi=val(substr(myString,AT("E",myString)+1,len(myString)))
** should get +10 / -10
** now power it up...
myNumber = mySignificant * (10 ^ Multi)
Return(myNumber)

** use it with the other bit from earlier...

Function GetVal
Parameter myString
Private myString,myNumber
if !"E"$Upper(myString)
myNumber = Val(StrTran(myString,",",""))
else
myNumber = Scientific(myString)
endif
return(myNumber)

** and for a purely integer format...
myInteger = Int(GetVal(myString))

** Does this help...
Griff
[smile]
ps I bet there's an easier way!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top