Display large number in compact notation in i18n compatible way (ex: display 122025999 as 122M or 1.2億) across browsers

Viewed 580

Need a cross browser compatible way to display compact notation for large numbers, very similar to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat with the notation: 'compact' parameters, currently in experiment mode. Or the chrome v8: https://v8.dev/features/intl-numberformat#notation

const formatter = new Intl.NumberFormat('en', {
    notation: 'compact',
  });

I want to use these compact notation number formatters with locale support, but they are not yet cross browser compatible.

Does anyone have a solution similar to this chrome implementation that also works in firefox, safari, etc.?

1 Answers

It’s actually quite simple, here’s Berezza’s method for abbreviating numerals ( Provides a lot more options than compact notation parameter );

> local Suffixes =
> {"k","M","B","T","qd","Qn","sx","Sp","O","N","de","Ud","DD","tdD","qdD","QnD","sxD","SpD","OcD","NvD","Vgn","UVg","DVg","TVg","qtV","QnV","SeV","SPG","OVG","NVG","TGN","UTG","DTG","tsTG","qtTG","QnTG","ssTG","SpTG","OcTG","NoTG","QdDR","uQDR","dQDR","tQDR","qdQDR","QnQDR","sxQDR","SpQDR","OQDDr","NQDDr","qQGNT","uQGNT","dQGNT","tQGNT","qdQGNT","QnQGNT","sxQGNT","SpQGNT",
> "OQQGNT","NQQGNT","SXGNTL"}                                           
> 
> 
> function Convert(Input)   local Negative = Input < 0  Input =
> math.abs(Input)
> 
>   local Paired = false    for i,v in pairs(Suffixes) do       if not (Input
> >= 10^(3*i)) then             Input = Input / 10^(3*(i-1))            local isComplex = (string.find(tostring(Input),".") and string.sub(tostring(Input),4,4)
> ~= ".")           Input = string.sub(tostring(Input),1,(isComplex and 4) or
> 3) .. (Suffixes[i-1] or "")           Paired = true           break;      end     end     if
> not Paired then       local Rounded = math.floor(Input)       Input =
> tostring(Rounded)     end     if Negative then        return "-"..Input   end
>   return Input -- returns 1.0k for example end Now to add commas to a

numeral , or to abbreviate it that style is a different function;

> function AbbreviateNumeral(numeral)   if numeral then         local
> left,num,right = string.match(numeral,'^([^%d]*%d)(%d*)(.-)$')
>       return left..(num:reverse():gsub('(%d%d%d)','%1,'):reverse())..right

-- returns for example 1,000, it gets every 3 zeros and adds a comma end end All you have to do is call one of these functions and it’ll return a number abbreviated!

Use Kensizo suggested. https://devforum.roblox.com/t/how-can-i-turn-a-number-to-a-shorter-number-i-dont-know-how-to-explain-click-to-understand-3/649496/3

In my game that is upcoming, Samurai Legends, it’s easier for players to understand Berezza’s conversion it seems to be, but whichever you prefer and think is cleaner is the one you should go with!

Unlike your method

it can also support negative numerals, which is pretty cool.

Related