Is there a way to test in a formula if a text field contains a valid number?

  • 0
  • 1
  • Question
  • Updated 9 months ago
  • Answered
I have a complex text formula field that extracts out text from another text field. If it contains a number, I want to use it in another formula field's calculations. If not, I want to skip over it and use a different text field's number. In MS Access there's a nice IsNumeric() function that will return true/false after testing a text string, but I see nothing of the sort in the function list for QuickBase. is there an easy, tricky way around this limitation?
Photo of Michael Barrow

Michael Barrow

  • 2,206 Points 2k badge 2x thumb

Posted 1 year ago

  • 0
  • 1
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,436 Points 50k badge 2x thumb
There may be other ways but this might be quick and dirty as a formula checkbox field.  But this will fail if the number has a leading zero like 0123.  If that is possible, then the formula will need to be a bit fancier.


ToText(ToNumber([maybe a number]))=[maybe a number]
Photo of Michael Barrow

Michael Barrow

  • 2,206 Points 2k badge 2x thumb
Thanks Mark. That's clever, but I couldn't get it to work. For my current purposes, this seems to have done the trick. 

Length([Subject Form ID]) > 0 and Left([Subject Form ID],1) >= "1" and Left([Subject Form ID],1) <= "9"

It really bugs me, though, that there isn't a native function to do something so straightforward and necessary.
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,436 Points 50k badge 2x thumb
I did test that formula and it did seem to work for me ... did you have a string that failed that I could test it on.  The useful idea is that the ToNumber( ) function will return blank it what you give it contains non numeric characters.

In my test, the field for [maybe a number] was a text string.
Photo of Surya Avantsa

Surya Avantsa

  • 218 Points 100 badge 2x thumb
That could be a problem. What if it was a blank? And the tonumeric also returns a blank. Then they both will be equal, thus misleading us to believe that a blank is numeric. 

I guess, we need to first filter out blanks and call them non-numeric right away before checking for non-blanks.

if (Contains([may be a number]," ",false,ToText(ToNumber([maybe a number]))=[maybe a number])
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,436 Points 50k badge 2x thumb
to test for a blank you would use

IF(Trim([my text field])="", true)

ie you test against empty quotes
Photo of Surya Avantsa

Surya Avantsa

  • 218 Points 100 badge 2x thumb
True. I agree.
Photo of Michael Barrow

Michael Barrow

  • 2,206 Points 2k badge 2x thumb
Yeah, I would have though it would work for me,but it didn't. The particular piece of text that was a problem was 'Oneil  (starting with an apostrophe). For my purposes, I am extracting FormID's which are all positive integers from 2 to 5 digits long, so I took the simple way to just test the first character, but I would like to get your more universal approach to work too, for what I'm sure will be later needs for this functionality.
Photo of QuickBaseCoach App Dev./Training

QuickBaseCoach App Dev./Training, Champion

  • 51,436 Points 50k badge 2x thumb
I tried 'Oneil on my formula and it correctly said it was not a number
(Edited)
Photo of Michael Barrow

Michael Barrow

  • 2,206 Points 2k badge 2x thumb
Hmm. Thanks for letting me know. I'll have to go back and do some more testing when I have time. Too much else going on with this project right now - I need to keep moving. Thanks again for your help!
Photo of Ⲇanom the ultimate (Dan Diebolt)

Ⲇanom the ultimate (Dan Diebolt), Champion

  • 26,522 Points 20k badge 2x thumb
I would avoid getting into a situation where you have either text or a numeric value in a text field in the first place. Rethink your fields and calculations. Ambiguous data (text/numbers) should be cleaned up at the point of key entry or importing.

You probably could do a simple formula knowing that a number in a business setting consists of a {set of digits} possibly proceeded by a plus or minus sign and at most one decimal point  amid the digits but it is just an exercise if difficulty.

QuickBase does not use scientific notation for numbers but technically you could have this in your string and ToNumber () will convert it correctly.

ToNumber("34e5") => 3400000

Or you could use JavaScript where any manner of parsing and data conversion is possible. Anyone here that could write a formula to accomplish the task you ask about could do it easier in JavaScript. Replace the formula language with JavaScript is my advice.

It strikes me as extremely odd that for a no code platform the first thing you have to do is master the formula language and all its quirks.   ̄\_(ツ)_/ ̄