admin管理员组

文章数量:1323157

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
Add a ment  | 

3 Answers 3

Reset to default 8

You 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:

  1. 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 €"

  1. 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>

  1. 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