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!

type mismatching - how to format strings as numbers for assigning

Status
Not open for further replies.

PureSoft

Programmer
Aug 12, 2002
24
GB
Hi,

I am pulling text from a file (csv export from a shopping cart) in string format, some of this text naturally represents numbers. I'm having problems assigning these strings as numbers to a new variable, originally defined as a variant. Here is the line along with my variable declarations:

Dim order_total
Dim hold_values() As String

order_total = FormatNumber(order_total, 2, , , vbFalse) + FormatNumber(hold_values(i), 2, , , vbFalse) * FormatNumber(hold_values(i + 1), 2, , , vbFalse) 'The running total order amount


What should I do with the declarations of the variables, or am I using the wrong formatting here?

I want to work with numbers formatted to 2 d.p.'s along the way (I need to round separately for each value, since the accounting package import procedure works with the separate values this way in order to balance).
 
Difficult without seeing further code but I think I see what you are getting at. What about using cdbl() instead of formatnumber()?
 
sorry, missed the rounding bit.

Why not try

order_total = round(order_total, 2) + round(hold_values(i), 2) * round(hold_values(i + 1), 2) 'The running total order amount

 
You should declare order_total and hold_values() as a doubles. Then do the math without the formatting and then format the result.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Thanks for the pointers, I found out that to convert a string to a number to use the Val() function, then coupled with the Round() function that does what I need. Here is an axample for anyone interested.

Dim order_total As Double
temp_hold_values() As String

order_total = order_total + (Round(Val(temp_hold_values(i)) * Val(temp_hold_values(i + 1)), 2)) 'The running total order amount


Philip.
 
You are better off using:

If Not IsNumeric(temp_hold_values(i)) Then temp_hold_values(i) = "0"

Val(Str$(temp_hold_values(i)))

in case the user has a different setting in the region settings for the decimal seperator.
Otherwise, the Val() function may return only the interger portion of the number.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top