2

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 comma so the figures do not become distorted?

Smac
  • 391
  • 2
  • 10
  • 28

3 Answers3

5

You could set the useGrouping option to false.

floatValue.toLocaleString(CurrentCulture, { minimumFractionDigits: 2, 
maximumFractionDigits: 2, useGrouping: false });

This should avoid the comma for thousand grouping in english and the dot for thousand grouping in german locale.

user3154108
  • 1,264
  • 13
  • 23
0

Since .toLocaleString() is language-sensitive, a manual replace is not recommended. 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 (recommended: 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.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
  1. Other options in the SO post How to print a number with commas as thousands separators in JavaScript .
0

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.

George Pant
  • 2,079
  • 1
  • 9
  • 14