Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Confused about str()

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.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Confused about str()

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.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,202
Default Confused about str()

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 dont include a leading space reserved for
the sign of the value; those converted using Str retain the leading
space."

Rick


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Confused about str()

Thanks for the replies - they were very helpful!

On Jun 3, 7:37 am, "Rick Rothstein \(MVP - VB\)"
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
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Now I'm Really Confused doss04 New Users to Excel 5 October 14th 08 04:38 AM
Very confused.... mizterbusy Excel Discussion (Misc queries) 1 September 24th 06 06:44 AM
Confused Karmen New Users to Excel 4 February 24th 06 08:27 PM
I'm Confused........................................ Jonathan Excel Programming 8 May 4th 05 12:37 PM
confused... keepitcool Excel Programming 0 September 22nd 03 09:29 AM


All times are GMT +1. The time now is 07:05 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"