javascript - 在浏览器中以i18n兼容方式以紧凑的符号显示大量数字(例如:将122025999显示为122M或1.2十亿)

标签 javascript localization internationalization cross-browser number-formatting

需要一种跨浏览器兼容的方式来显示大量的紧凑表示法,非常类似于https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat的表示法:“compact”参数,当前处于实验模式。或Chrome V8:https://v8.dev/features/intl-numberformat#notation

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

我想将这些紧凑的符号数字格式器与区域设置一起使用,但它们尚未与跨浏览器兼容。

有没有人有与此Chrome实现类似的解决方案,也可以在Firefox,Safari等中使用?

最佳答案

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!


使用Kensizo建议。
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!


与您的方法不同

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

关于javascript - 在浏览器中以i18n兼容方式以紧凑的符号显示大量数字(例如:将122025999显示为122M或1.2十亿),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58598598/

相关文章:

javascript - 是否可以在 url 路径的参数值中发送特殊字符?

java - 在 android 中创建区域设置特定目录的 -b+ 标志是什么?

javascript - 使用css在某一类元素下方添加图像

javascript - 无法使用服务人员缓存简单的 html 文件

ios - 当我更改 iPhone 的语言时,会导致崩溃吗?

localization - 使用 ToString ("C"时,Blazor 显示 ¤ 而不是 $

PHP IntlDateFormatter->format() 返回 false

reactjs - 对于 Redux 存储来说多大才算太大?

.net - 如何在 .NET 中使用非英文引号进行格式化?

javascript - 在单独的渲染之间共享 React 组件逻辑