admin管理员组文章数量:1323681
I am currently trying to deal with numbers for both English (en) and German (de) languages. I have a global function which does this and works fine until the number reaches 1000 or more.
function getLocaleString(floatValue) {
var CurrentCulture = $('#CurrentCulture').val();
if (CurrentCulture != "" && CurrentCulture != null) {
return floatValue.toLocaleString(CurrentCulture, { minimumFractionDigits: 2,
maximumFractionDigits: 2 });
}
return floatValue;
}
Is there a way to get this to remove the ma so the figures do not bee distorted?
I am currently trying to deal with numbers for both English (en) and German (de) languages. I have a global function which does this and works fine until the number reaches 1000 or more.
function getLocaleString(floatValue) {
var CurrentCulture = $('#CurrentCulture').val();
if (CurrentCulture != "" && CurrentCulture != null) {
return floatValue.toLocaleString(CurrentCulture, { minimumFractionDigits: 2,
maximumFractionDigits: 2 });
}
return floatValue;
}
Is there a way to get this to remove the ma so the figures do not bee distorted?
Share asked Feb 13, 2020 at 15:41 Smac Smac 4012 gold badges13 silver badges29 bronze badges 2-
1
return floatValue.replace(/,/g, '')
– Yuvaraj G Commented Feb 13, 2020 at 15:48 - var tmp ='1,000'; tmp.replace(/,/g, ''); – vadivel a Commented Feb 13, 2020 at 15:59
3 Answers
Reset to default 8You could set the useGrouping
option to false.
floatValue.toLocaleString(CurrentCulture, { minimumFractionDigits: 2,
maximumFractionDigits: 2, useGrouping: false });
This should avoid the ma for thousand grouping in english and the dot for thousand grouping in german locale.
Since .toLocaleString()
is language-sensitive, a manual replace is not remended. Different languages may use different symbols at different locations, as intended. Use a proper number formatter instead.
The most user-friendly and consistent options, without unnecessary regex:
- Use
Intl.NumberFormat
(remended: no extra dependency):
const number = 123456.789;
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// expected output: "123.456,79 €"
- Use Numeral.js.
console.log(numeral('10,000.12').format('$0,0.00'));
console.log(numeral(1000).format('0,0'));
<script src="//cdnjs.cloudflare./ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
- Other options in the SO post How to print a number with mas as thousands separators in JavaScript .
You can just use replace()
. Javascript's String replace method
floatValue.toLocaleString(CurrentCulture, {minimumFractionDigits: 2,
maximumFractionDigits: 2 }).replace(/,/g, "");;
g
modifier is used for global replacement, in order to replace all occurences of the specified value.
本文标签: jqueryHow do I remove comma for Javascript function toLocaleStringStack Overflow
版权声明:本文标题:jquery - How do I remove comma for Javascript function toLocaleString - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742141920a2422616.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论