PC Review


Reply
Thread Tools Rate Thread

Confused about str()

 
 
Ferris
Guest
Posts: n/a
 
      1st Jun 2007
I'm curious to know as to why if:

x = 1

str(x) = "1" equates to FALSE.

I've tried x as Long, Integer, etc. Does anyone know why?

Thanks.

 
Reply With Quote
 
 
 
 
=?Utf-8?B?SmltIFRob21saW5zb24=?=
Guest
Posts: n/a
 
      1st Jun 2007
The str function leaves a leading space for the sign as per its description

"Remarks

When numbers are converted to strings, a leading space is always reserved
for the sign of number. If number is positive, the returned string contains a
leading space and the plus sign is implied."

If you don't want the leading space use CStr function...

Sub test()
Dim x As Integer

x = 1
MsgBox str(x) = " 1" 'equates to true
End Sub

Sub test()
Dim x As Integer

x = 1
MsgBox Cstr(x) = "1" 'equates to true
End Sub
--
HTH...

Jim Thomlinson


"Ferris" wrote:

> I'm curious to know as to why if:
>
> x = 1
>
> str(x) = "1" equates to FALSE.
>
> I've tried x as Long, Integer, etc. Does anyone know why?
>
> Thanks.
>
>

 
Reply With Quote
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      3rd Jun 2007
> The str function leaves a leading space for the sign as per its
> description
>
> "Remarks
>
> When numbers are converted to strings, a leading space is always reserved
> for the sign of number. If number is positive, the returned string
> contains a
> leading space and the plus sign is implied."
>
> If you don't want the leading space use CStr function...


You can also use the Format function (although, since we know we want a
String value back, I would use Format$ of the function instead).

x = 1

Format$(x) = "1" --- evaluates to True

This is documented in the Remarks section of the help file for the Format
Function...

"If you try to format a number without specifying format, Format
provides functionality similar to the Str function, although it is
internationally aware. However, positive numbers formatted as
strings using Format don’t include a leading space reserved for
the sign of the value; those converted using Str retain the leading
space."

Rick


 
Reply With Quote
 
Ferris
Guest
Posts: n/a
 
      7th Jun 2007
Thanks for the replies - they were very helpful!

On Jun 3, 7:37 am, "Rick Rothstein \(MVP - VB\)"
<rickNOSPAMn...@NOSPAMcomcast.net> wrote:
> > The str function leaves a leading space for the sign as per its
> > description

>
> > "Remarks

>
> > When numbers are converted to strings, a leading space is always reserved
> > for the sign of number. If number is positive, the returned string
> > contains a
> > leading space and the plus sign is implied."

>
> > If you don't want the leading space use CStr function...

>
> You can also use the Format function (although, since we know we want a
> String value back, I would use Format$ of the function instead).
>
> x = 1
>
> Format$(x) = "1" --- evaluates to True
>
> This is documented in the Remarks section of the help file for the Format
> Function...
>
> "If you try to format a number without specifying format, Format
> provides functionality similar to the Str function, although it is
> internationally aware. However, positive numbers formatted as
> strings using Format don't include a leading space reserved for
> the sign of the value; those converted using Str retain the leading
> space."
>
> Rick



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused Newbie - Very Confused:( Wizard Microsoft Access Forms 0 20th Oct 2005 10:37 AM
Confused! Kevin M Asus Motherboards 1 16th Aug 2005 05:24 PM
Confused =?Utf-8?B?QmxpbmU=?= Microsoft Access 1 5th Aug 2005 05:31 PM
Confused Steven Ridley Microsoft Outlook 1 18th Apr 2004 06:26 AM
Confused, need valid logon for OE, confused.. Rob Windows XP General 1 3rd Nov 2003 02:15 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:13 AM.