Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Diff. between functions "Val" & "Cint", similarly "Str" & "CStr" ?

204 views
Skip to first unread message

Mustansir

unread,
Sep 11, 2002, 3:04:24 AM9/11/02
to
I want to know what is the difference between the functions "Val" & "Cint"
because I noticed that both convert to a number. Similarly, "Str" & "CStr"
convert to a string. So whats the point in having 2 functions that do the
same task.

Mustansir

Jeff Johnson

unread,
Sep 11, 2002, 8:40:09 AM9/11/02
to

"Mustansir" <must...@hotvoice.com> wrote in message news:#qxPe5YWCHA.1912@tkmsftngp09...

Legacy and functionality. Go to the immediate window and type:

? Val("45x")
? CInt("45x")

Now do this and compare how these numbers line up:

? Str(1);vbCrLf;CStr(1);vbCrLf;Str(-1);vbCrLf;CStr(-1)

And by the way, Val() returns a Double, so it's closer to CDbl() than CInt().


Adam D. Barratt

unread,
Sep 11, 2002, 9:06:43 AM9/11/02
to
In microsoft.public.vb.general.discussion Jeff Johnson <pawprint@your_clothes.geocities.com> wrote:
>
> "Mustansir" <must...@hotvoice.com> wrote in message news:#qxPe5YWCHA.1912@tkmsftngp09...
>
>> I want to know what is the difference between the functions "Val" & "Cint"
>> because I noticed that both convert to a number. Similarly, "Str" & "CStr"
>> convert to a string. So whats the point in having 2 functions that do the
>> same task.
>
> Legacy and functionality.

Plus return type in many cases and a rounding/truncation distinction for
the numeric conversions.

e.g. Str() returns a Variant (String); CStr() returns a String.
Int() truncates, CInt() Bankers' rounds

Adam
--
Excusing bad programming is a shooting offence, no matter _what_ the
circumstances.
-- Linus Torvalds, to the linux-kernel list

Matthew Curland

unread,
Sep 11, 2002, 4:11:28 PM9/11/02
to
Among other things, Str/Val assume that the data is in English format (. for
a decimal and , for a separator). CStr/CInt(CDbl, ...) interpret the data
according to the current users settings. Unless you're writing data that
needs to be readable on an arbitrary language machine (always a good idea if
your data is leaving the box it was created on), the you should use Str/Val.
Most of the time, CStr/CInt(etc) are the better choice. Val is also not a
very fast function compared to the others. -Matt

"Mustansir" <must...@hotvoice.com> wrote in message
news:#qxPe5YWCHA.1912@tkmsftngp09...

David Marfleet

unread,
Sep 12, 2002, 2:46:02 PM9/12/02
to
Val returns the numbers in a string to a numeric value of the appropriate
type, whereas CInt converts a value to a 16-bit integer. Val retains the
decimal portion of a number, but CInt retains only the integral portion, eg.
Val("10.5") = 10.5 but CInt("10.5") = 10. Str is specifically used for
returning the string representation of a number, whereas CStr is a
general-purpose function for converting any value to a string.

Regards

Paul

"Mustansir" <must...@hotvoice.com> wrote in message
news:#qxPe5YWCHA.1912@tkmsftngp09...

Mustansir

unread,
Sep 14, 2002, 7:35:03 AM9/14/02
to
Couldn't understand what you were trying to impress upon. Please elaborate a
little as I didn't get your point even after trying it out.

Mustansir

Eduardo A. Morcillo [MVP]

unread,
Sep 14, 2002, 2:23:15 PM9/14/02
to
> Couldn't understand what you were trying to impress upon. Please
> elaborate a little as I didn't get your point even after trying it
> out.

Str adds space for the sign. When the number is positive the string contains
a space at the beginning. CStr does not add that extra space.

--
Eduardo A. Morcillo [MS MVP-VB]
http://www.domaindlx.com/e_morcillo


0 new messages