Len() of A Number Mystery

raskew

AWF VIP
Local time
Today, 00:18
Joined
Jun 2, 2001
Messages
2,731
Have a function that takes a number as a string, e.g.
x = "123.45"

...then continues to use the len(x) as part of a calculation.
Stepping through it, it returns len(x) as 8. Same if done
from the debug/immediate window:
? len(x)
8


Experimented with a number, e.g.
y = 123.45
? len(y)
6


I'm sorely mystified. Can someone explain how Access arrives
at these results.

Thanks - Bob
 
Last edited:
Hi Bob,

Your function definition has the parameter defined as a Number.

It will always return 8 (it's a double).

At least I think that's what's happening.

Good to see you again,
Wayne
 
Hi Wayne -

Long time, no see. I'm not sure (no offense intended) of
your explanation, but while floundering-around, trying to
find a way to implement your guidance, I found that:

x = "123.45"
y = val(x)
? len(y)
6

...which kind of matches up with:
y = 123.45
? len(y)
6

I'm still confused, but maybe I've stumbled on a work-around.

Keep your head down, those wild-fires could move South.

Best Wishes - Bob
 
Bob,

No worries here, the heat wave is over ... back to normal.

I think that this all revolves around how you have it declared in your function
definition.

x = "123.45"
y = val(x)
? len(y)
6

Is y a variant?

I can't see it returning 6!

Wayne
 
Wayne -

Give it a try in the debug window:

x = "123.45"
y = val(x)
? len(y)
6

Just tried it again and it returned 6.

Haven't tried it in the function, but it should behave
the same. Where this came from was I was attempting
to answer a request in another forum where the OP wanted
to convert a number to the text in a check, e.g.
"One hundred twenty-three and forty-five cents".

I've got a number of routines that do this but none of them
do the cents business. Went to the first one, which I thought
I'd written (looked like my code and I hadn't included an attribution
line) and ran into this issue. Since it didn't make sense to me,
I obviously hadn't written the code (duh!). Hence the question.

Stepped through it and immediately ran into the len() issue.

Please post back and let me know if you're getting the same results.

Bob
 
Bob,

Sorry, got sidetracked yesterday.

If y is undeclared, therefore a variant, it will have a context-sensitive value.

y = Val("12345.67") <-- y is assigned the number 12345.67, the datatype is
still a variant.

y = y + 3 <-- y is treated as a number and equals 12348.67, the datatype is
still a variant.

Len(y) <-- y is treated as a string (that's what the Len function wants) and
its length = 8.

BUT, if you Dim y as a Single, or Double, the Len function will ALWAYS return
either 4 or 8, because that's the size of the data structure.

I don't think y was declared as a specific data type in your example.

Wayne
 
Thanks Wayne -

In the function, y is dimensioned as Single. So, with your patient explanation, len(y) = 8 is correct.

Thanks again - Bob
 

Users who are viewing this thread

Back
Top Bottom