简体   繁体   English

JavaScript:toLocaleString()

[英]JavaScript: toLocaleString()

My JavaScript toLocaleString() is doing something strange here.我的 JavaScript toLocaleString()在这里做了一些奇怪的事情。 If I have a number less than 1000, it will give dot as decimal delimiter, but if I have a number more than 1000, it will give me comma as decimal delimiter.如果我有一个小于 1000 的数字,它会给我点作为十进制分隔符,但如果我有一个大于 1000 的数字,它会给我逗号作为十进制分隔符。

My locale setting uses dot as thousand delimiter and comma as decimal delimiter.我的语言环境设置使用点作为千位分隔符,逗号作为十进制分隔符。

This is how I use the code:这是我使用代码的方式:

sellingPrice.toLocaleString(undefined, { minimumFractionDigits: 2 }

If sellingPrice is 700 , it will give me 700.00 If sellingPrice is 7000 , it will give me 7.000,00如果 sellPrice 是700 ,它会给我700.00如果 sellPrice 是7000 ,它会给我7.000,00

But the result is the same但结果是一样的

Why is this happening?为什么会这样?

I just found out that javascript is following the browser locale.我刚刚发现 javascript 遵循浏览器语言环境。

In my case I use chrome.就我而言,我使用铬。 I have to change the language to my language (Indonesia) to achieve my goal, which is a bit disappointing.我必须将语言更改为我的语言(印度尼西亚)才能实现我的目标,这有点令人失望。 I want my browser language to be English.我希望我的浏览器语言是英语。

I cannot use "en" or "de" because I want the code to be dynamic and follows the user locale.我不能使用“en”或“de”,因为我希望代码是动态的并遵循用户语言环境。

var sellingPrice=700;sellingPrice.toLocaleString('en-us', { minimumFractionDigits: 2 });

will give you the result 700.00会给你结果700.00

and

var sellingPrice=7000;sellingPrice.toLocaleString('en-us', { minimumFractionDigits: 2 });

will give you the result 7,000.00会给你结果7,000.00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM