I fetch around 1000 different numbers from a CMS to display them in my frontend. I want to display anything above 1000 as 1k and so on. There are only full numbers fetched and no decimals.
Few examples: 1200 = 1.2k, 1209 = 1.2k, 18094 = 18.1k 999 = 999
So in the first place i have to check every number if it has more than 3 digits, if so it should round correctly and after it append a "k" to the number (now string in this case). The first step is clear to me but i struggle a bit with the correct rounding. The solution should also be as most cost efficient as possible. The solution needs to be in JS.
Maybe someone can help me out, thanks in advance!