Ordinal Numbers using javascript

I have some numbers in a data type such as

  • User
  • Percentile Rank

The Percentile rank is something like 0.023, which I can format easily enough as 2%. However, I want to present text likie-
“You scored in the 2nd percentile”.
or “You scored in the 13th percentile.”
or “You scored in the 98th percentile…”

I see there is this topic here,

which is close but not really helping me out.

There is this post on stackOverflow for how to write javascript to convert numbers to the ordinal format

Is it possible to use the Toolbox plugin to run custom javascript that will format the specific numbers I want to format?

Or any other way to accomplish this?

Thanks,
Chris

1 Like

Chris,

I think there is a Bubble way to do this but I honestly find the forums difficult to track down information and I am feeling too lazy to look for it. To answer your question re the Toolbox plugin, yes it can easily be done.

You could use the run javascript but if you using it for display only I would use the “Expression” element from the Toolbox plugin. Posting the code below and then you use Expression A's value

Use this as the expression (I conformed it works as expected):

function nth(n){return["st","nd","rd"][((n+90)%100-10)%10-1]||"th"}
nth(10);

note: I hardcoded 10 ( in nth(10); ) for testing but this is where you put your dynamic expression or value that you are wanting to convert.

Hi @bubble.trouble , thanks for this suggestion. My first time using the Expression element and it’s not displaying anything for me.

I can see it in the debug but I can’t change the style, it looks transparent or something.

It holds the computed value. You reference the value to display it somewhere.

ExpressionA’s value