开发者

Does Number.toFixed() use locale conventions?

开发者 https://www.devze.com 2023-02-27 08:42 出处:网络
I am writing a JavaScript function that can perform decimal place manipulation on a string that was generated from a number using Number.toFixed().Part of what I need to accomplish is to locate the lo

I am writing a JavaScript function that can perform decimal place manipulation on a string that was generated from a number using Number.toFixed(). Part of what I need to accomplish is to locate the location开发者_开发问答 of the decimal point in the formatted string. I have begun to wonder, however, whether the toFixed() function will use locale specific formatting (e.g. '.' versus ',' for the decimal point). If that is the case, I can't merely search for the '.' character but may need to search for a comma or, perhaps, some other separator that is used in other locales.

In C++, I can use the numpunct locale facet to determine what character is used but I don't believe that there is any equivalent resource available in JavaScript. My question then becomes whether toFixed() is going to honour locale conventions and, if so, is there any way to recognise what convention is used on the browser host?


No, i does not. Only .toLocaleString() respects decimal separator. Neither Number('42,6') will return proper value assuming locale have ',' as decimal separator.


// You can always ask-

Number.decimalChar= (function(){
    return (3/5).toLocaleString().charAt(1)
})()

alert(Number.decimalChar)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号