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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

converting string values to numeric

Status
Not open for further replies.

Atlas

Programmer
Sep 20, 1999
26
US
I have situation where I am receiving data from a spreadsheet where the numeric data is received as a character string Example 1,884.44

When I try to convert to numeric string I end up with a value of 1. I have tried different functions and still get the same result. All I want to do is save this number as a rounded numeric value ie: 1884.

I would appreciate any help.

Thanks

Atlas
 
A slight modification to the above...

Code:
mcNumber = "1,234.56"

* --- Remove Comma's From Char String ---
mcNumber = STRTRAN(mcNumber,",","") 

* --- Convert Char to Number ---
mnNumber = VAL(mcNumber)

* --- Or Convert Char to RoundUp Number ---
mnRndNumber = INT(VAL(mcNumber) + 0.5)

Good Luck,
JRB-Bldr
 
a one-line variation of the above:
Code:
? VAL(STRTRAN(mcNumber, ",", ""))
i hope this helps. peace! [peace]

kilroy [trooper]
philippines

"Illegitimis non carborundum!"
 
Don't know about the performance difference, but the one character replacement function is CHRTRAN().

Brian
 
Atlas,
Given that you said you wanted the value rounded I would use:
Code:
ROUND(VAL(CHRTRAN("1,844.3",",","")),0)

torturedmind
I don't see where your suggestion is different than the one lordhawkins made

baltman
There is indeed a performance difference in favor of chrtran()...even putting strtran() second in the test so it perhaps picks up some benefit from cache it is shown to be slower than chrtran()

Code:
lnStartSeconds = SECONDS()
FOR lnCounter = 1 TO 1000000
	ROUND(VAL(CHRTRAN("1,844.3",",","")),0)
ENDFOR
?SECONDS() - lnStartSeconds

lnStartSeconds = SECONDS()
FOR lnCounter = 1 TO 1000000
	ROUND(VAL(STRTRAN("1,844.3",",","")),0)
ENDFOR
?SECONDS() - lnStartSeconds

boyd.gif

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top