Generating an emoji star chart (using text) from a variable ranging from 0 to 5

Hello

so in discord you can get custom emoji:

  • :star_full:
  • :star_empty:
  • :star_half:

star-full
star-empty
star-half

Examples: how a human would do it

in order to display, for example, 4.5 stars, I would need to type:

:star-full::star-full::star-full::star-full::star-empty:

in order to display, for example, 1star, I would need to type:

:star-full::star-empty::star-empty::star-empty::star-empty:

Generating the second variable (essentially text, from a number)

  • I have the numerical star rating stored as a variable: %Variable%stars_how_many%
  • I wish to create a second variable called %Variable%stars_rendered_for_discord%

Is there a formula or technique to get every single combination? (0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0, 4.5, 5.0) Actually, come to think of it, the number could be anything between 0-5, i would need to round up or down to the nearest 0.5... I know how to do that using KM.

I can't think of a way to generate the new variable? Can I use a forumula to create the new variable?

Is there a way to represent 5x as xxxxx, or 3x as xxx ?

If I knew how to do this then I could get started

I'm sure there's a lot of ways to do this! If you already have the number rounded to the nearest .5, you could just use a TRUNC function in combination with pulling the decimal digit from the variable as an array with '.' as the custom separator, along with a few loops. This is using global variables, so you'd need to switch to local variables or clear them to use it repeatedly.

This also doesn't have any error checking, so it'll fail if you don't have a decimal in the variable, just a quick sketch of one way to approach it.

Stars.kmmacros (6.1 KB)

edit: typo

2 Likes

please explain what is happening here:

set variable
stars_how_many_dec

to
%Variable%stars_how_many[2].%

What does adding [2]. the end do?

  • are you adding '[2]` to create a new variable name?
  • Does the . have to be after it?
  • or does this extract the decimal value ? I've never seen this before. Is it in the wiki?

Thanks!

You can access any variable as an array.

So in %Variable%stars_how_many[2].%

'2' is the index number of the variable array, with '.' being the custom delimiter for the array.

1 Like

okay thanks, i will research what an array is! I've heard of it before.

I'm confused by your Empty Stars block. it doesn't have any conditions before it, How come it's going direct to "execute these actions"?

Your macro definitely works! But I need to break it down and try to understand every step (it will help me in future)

I am reading your macro and trying to understand it like a flowchart.

Why are you doing:

set variable
stars_how_many_whole

to stars_how_many_whole+1 ?

I can see it's adding one ... but why?

The 'Empty Stars' block, is a repeat action! I just renamed it.

It's set to execute (5-stars_how_many_whole) times, so if there are 5 full stars, it will execute 0 times.

In that if statement, if there is a .5 in the stars_how_many variable, the stars_how_many_dec, will be '5', so the condition will succeed, add a half star to the output variable, and increment the 'stars_how_many_whole' variable, so that the number of empty stars will be reduced by 1, since a half star should take the space of an empty star, which is calculated in the repeat action "Empty Stars"

Apologies if this was a little obtuse! I didn't do a lot of labeling, or any commenting, this was just a rough sketch. It might not be super clear what i was going for

Where or how are you telling KM to execute it x times?

Doesn't KM just go down an execute each block in sequence?

Please take a look at this

for Evan 21.02.2024.kmmacros (8.2 KB)

It is working perfectly apart from the empty stars code block. (I suspect I broke it by accident.)

You will see I've tweaked some things, including the emoji names which should have had underscores instead of dashes.

(BTW, when I upload a KM macro, the forum doesn't generate a screenshot of all the steps like it did in the OP)

Not necessary, as long as i am able to ask you little things that I don't understand then I can figure it out! :slight_smile:

you have VERY KINDLY given me the answer on a plate. I can dissect it myself.

Thank you again for taking the time to do all this!

When a "Repeat" action is renamed, it removes the word "times" from the UI, which can make it a little confusing.

Making a fresh repeat action without renaming it looks like this:

The field "execute these actions" is a calculation field, for how many repeats the loop will do.

the only thing you missed was you didn't change the variable in the repeat action to instance_stars_how_many_whole

You can copy the whole macro, or any number of actions as an image by selecting it in the editor, and selecting "Copy as image".

Also there are several macros people have made to automate this, one i've used is:

As well as the built in "share to forum" functionality, which uses an API key.

You're welcome :slight_smile:

1 Like

4.6 Stars is rendering as 4 stars.. unless it's a weird bug

Screenshot 2024-02-22 at 16.59.57

I think 4.5 stars would be more apt?

EDIT: i think it's because TRUNC(4.6) is 4?

EDIT 2: skimming over the code, it seems that if it had been 4.5 then it would have correctly rendered the half star at the end.. but the code doesn't look for that

possible solution?

Screenshot 2024-02-22 at 17.08.57
Could I just add if the variable is 6 etc. ?

like this:

You mentioned in your original post that you already knew how to round to the nearest .5, so i didnt' include any logic to do so. I think you're better off rounding the initial number to your taste, since adding the conditionals like you did here wouldn't round UP from 4.7, 4.8, 4.9 to 5 stars. Or just only taking increments of .5 as an input.

1 Like

eh it's fine, this is certainly good enough :slight_smile:

. oops wrong thread. deleted

. oops wrong thread