Weird (?) 'Str' function

Post Reply
Sighurd
Posts: 30
Joined: Mon Oct 03, 2011 9:18 pm
OLAP Product: TM1
Version: 10.2.2fp6
Excel Version: 2016

Weird (?) 'Str' function

Post by Sighurd »

I was using Str function in my TI process and had unexpected results. I started investigating and simplifying the case, and this is the test case I finally came to:

Code: Select all

R = 123456789.5123;
P = 123456789.888;

filePath = 'D:\ProcessOutput\output.txt';

TextOutput (filepath,  'R - '|Str (R,3,1), 'P - '|Str (P,4,1));
Please note the Length parameter of Str function - 3 for К and 4 for P.

This is what my output.txt looks like:

Code: Select all

"R - ","P - 123456789.9"
How does it actually work? :shock: Why is 'R' empty? What is the difference between 3 and 4 for Length to produce these vastly different results?

TM1 10.2.2fp6
User avatar
Alan Kirk
Site Admin
Posts: 6606
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: Weird (?) 'Str' function

Post by Alan Kirk »

Sighurd wrote:I was using Str function in my TI process and had unexpected results. I started investigating and simplifying the case, and this is the test case I finally came to:

Code: Select all

R = 123456789.5123;
P = 123456789.888;

filePath = 'D:\ProcessOutput\output.txt';

TextOutput (filepath,  'R - '|Str (R,3,1), 'P - '|Str (P,4,1));
Please note the Length parameter of Str function - 3 for К and 4 for P.

This is what my output.txt looks like:

Code: Select all

"R - ","P - 123456789.9"
How does it actually work? :shock: Why is 'R' empty? What is the difference between 3 and 4 for Length to produce these vastly different results?

TM1 10.2.2fp6
It's TM1's way of saying to you "I honestly don't know what the hell you're trying to do here, so here's my best guess".

The core problem is that neither of those functions make any real-world sense when used with those arguments. The Str() function has three arguments:
- The number being converted to a string;
- The length of the string. If necessary, the function inserts leading blank spaces to attain this length; and
- The number of decimal places to include in the function result.

The bit that's added to the description of the second argument is the clue. The argument should be greater than or equal to the number of digits in the number being converted. If the number of digits is more than that, the original will be returned as it is (but with the decimals rounded) down to a length value of 4. If the length value is 3 or below it just throws up its hands and walks away.

Why the coders of the function decided to set the limit at a length of 4 for this I have no idea but the fact remains that if the numbers are vastly longer than the length that you've specified then it makes no sense anyway. You want a length of 3 or 4 out of a 12 or 13 digit value; any particular 3 digits? The ones on the left? The ones on the right? A few plucked at random out of the middle? And then to those three or four digits we add a rounding to the right of the decimal place. No, that doesn't seem like a particularly useful formula result.

It may be an idea to specify what your objective is here. I suspect that what you actually need is some combination of SubSt(), NumberToString and/or NumberToStringEx() but without knowing the full context there is no way to tell.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
Sighurd
Posts: 30
Joined: Mon Oct 03, 2011 9:18 pm
OLAP Product: TM1
Version: 10.2.2fp6
Excel Version: 2016

Re: Weird (?) 'Str' function

Post by Sighurd »

Alan Kirk wrote: It's TM1's way of saying to you "I honestly don't know what the hell you're trying to do here, so here's my best guess".

The core problem is that neither of those functions make any real-world sense when used with those arguments. The Str() function has three arguments:
- The number being converted to a string;
- The length of the string. If necessary, the function inserts leading blank spaces to attain this length; and
- The number of decimal places to include in the function result.

The bit that's added to the description of the second argument is the clue. The argument should be greater than or equal to the number of digits in the number being converted. If the number of digits is more than that, the original will be returned as it is (but with the decimals rounded) down to a length value of 4. If the length value is 3 or below it just throws up its hands and walks away.

Why the coders of the function decided to set the limit at a length of 4 for this I have no idea but the fact remains that if the numbers are vastly longer than the length that you've specified then it makes no sense anyway. You want a length of 3 or 4 out of a 12 or 13 digit value; any particular 3 digits? The ones on the left? The ones on the right? A few plucked at random out of the middle? And then to those three or four digits we add a rounding to the right of the decimal place. No, that doesn't seem like a particularly useful formula result.

It may be an idea to specify what your objective is here. I suspect that what you actually need is some combination of SubSt(), NumberToString and/or NumberToStringEx() but without knowing the full context there is no way to tell.
Alan, thank you for reply.

I understand that this exercise looks as stupid as it gets :D I promise you the original task had much more to do with real life than this one. I tried to simplify it and get to the core of the problem.

I am sorry if it seemed that I did not read the reference manual on STR function - I assure you I did several times. Your explanation (The argument should be greater than or equal to the number of digits in the number being converted) should have been there, but it's not. It just doesn't make sense to me. I mean - the empty string for 'R'.

And the arbitrary choice of Length of 4, but not 3 for different kinds of behavior does not make any sense either.

When It says that the second argument is Length - I expect Str (123456789.888,3,1) to produce '123', or '789', or even '9.9'. But not the empty string, or the whole string like in case with 'P'. Both 'P' and 'R' seem to use Length in some very unexpected way. And to actually get '123' I have to do it like "SUBST (TRIM (Str (var,15,0)),1,3)". And to get "9.9" would involve LONG and some calculation :)

In short - the function should be described better. I hope we can agree on that :)

Regarding the actual problem - there was an hour of frustration about things not mapping right, and then a workaround solution, which works even better. And I learned a couple tricks along the way, so it's a happy end story :)
Post Reply