Convert String to Number for Filtering
I noticed that I can't filter by text string fields. I keep file numbers in string fields and they look like 99955.023. I want to filter by the prefix 99955. I am able to pull that substring out, but I get the error that the output needs to be a number. How can I convert the variable to a number so Podio calculation field won't reject it? Thank you!
-
Hi Brett,
you need to use a JavaScript Number-object. Try this:
Number(@your_text_field)
Note that you probably have to create a new calculation field for this to work, because Podio is kind of picky when it sees the output change from text to numbers.
Best,
Stefan -
Thanks for the quick reply! I tried and I get the following error:
"The result is not a valid number"
Here is my code:
ID = @All of File No.[0].split(".",1) + ""
write = Number (ID)
The field is a number field. When I create a new field and put in the same code, it seems to make it a text field, which doesn't come up in the filtering options. I tested the value of ID and it shows 99,794 (which is the 99794 prefix in a file no 99794.001).
Any idea why it may not be a valid number?
-
Hi Brett,
sorry, I forgot this might happen. Podio sometimes uses random "test-data" in calculations which usually is helpful, but in some cases it's not. :/
When you user Number(x) with an x that really is NOT a number, this results in a special Javascript-Number instance "NaN" (short for NotaNumber) and Podio will refuse to save the calculation.
To avoid this, return -1 or any number if isNaN(Number(x)) is true, else return Number(x):
var your_text_field = @Titel; no_dots = your_text_field.replace(".", ""); var as_number = Number(no_dots); if (isNaN(as_number)) { -1; } else { as_number; }
Note: once you successfully saved the calculation and it works, you can change this line to return null instead, if you want.
Hope this helps! Best,
Stefan -
Just for information: Christian Holm, the former Podio-CDO, gave me this advice, to force the calculation field to accept a NaN result as number so you can save it:
parseInt(no_dots, 10) || 0
" || 0" does the same as:
if (isNaN(as_number)) {
-1;
but it's faster to write :) .Rainer
-
Just before I found this post, I screamed into the air, "Where is Rainer? He would know."
Rainer, You always have the answers. Every time. The Podio Demi-Diety. There is also Andreas (either one), but you guys are all on the same level in my book. If I was on myspace still, I would put you in my top 8....maybe 4...if I could convince you to be my friend on myspace.
:Werenotworthywayne'sworldgif:
I could probably tone it down with a simple thank you.
Please sign in to leave a comment.
Comments
8 comments