3

We are developing application which should support both German and English users.

What can we do to allow German users to enter numbers in German format (, as decimal point and . as thousand separator), and moment he tab out from input box, we should be able to convert input text into JavaScript number, Right now when we use Number() function it gives us the NaN for Number("12,23")

We are able to convert English text to German format number using new Intl.NumberFormat() but it is vice versa which we want.

matisetorm
  • 857
  • 8
  • 21
Anand
  • 4,523
  • 10
  • 47
  • 72
  • `123,456` now is that onehundredthousand or hundred and a fraction? – Jonas Wilms Dec 13 '17 at 06:18
  • You might need to store the value which the parser understands and mutate it just for displaying purpose. – Chay22 Dec 13 '17 at 06:19
  • You will have to convert it yourself. JavaScript does not provide such method. – Derek 朕會功夫 Dec 13 '17 at 06:19
  • @derek i know. However its probably quite hard to distinguish both ways of writing without asking the client for his location – Jonas Wilms Dec 13 '17 at 06:22
  • @JonasW. that is hundred and a fraction. – Anand Dec 13 '17 at 06:23
  • I believe i must be missing something basic, as this ask must be common to all i8n supporting apps in JS, isn't it? – Anand Dec 13 '17 at 06:27
  • 1
    I think you have to look into localization of your react app, define a locale for your app. If you want it to work based on users current language on their machine or browser then it is already working like that, if you want to dynamically change the language of the app you should look into localization – Bhupendra Dec 13 '17 at 06:29

1 Answers1

2

you could use numeral.js locales http://numeraljs.com/#locales

// load a locale
numeral.register('locale', 'de', {
    delimiters: {
        thousands: '.',
        decimal: ','
    },
    abbreviations: {
        thousand: 'k',
        million: 'm',
        billion: 'b',
        trillion: 't'
    },
    ordinal : function (number) {
        return number === 1 ? 'er' : 'ème'; //this is taken from french example, don't know in german
    },
    currency: {
        symbol: '€' //same as above
    }
});

// switch between locales
numeral.locale('de');

console.log(numeral('123,45').value());
console.log(numeral('6.123,45').value());
console.log(numeral('123,45m').value());
<script src="//cdnjs.cloudflare.com/ajax/libs/numeral.js/2.0.6/numeral.min.js"></script>
am05mhz
  • 2,727
  • 2
  • 23
  • 37
  • numeral.js seems dead, the last commit is 6 years old (on Mar 27, 2017) – DLight Mar 20 '23 at 14:39
  • @DLight is a formatting lib need recent update? what update? new feature? bug fix? does it have bugs? does it need new features? does it have vulnerability? if the answer to those questions is no, then there is no problem using 6 years old code, even MD5 have no updates for several decades, but its still being used where it fits the need – am05mhz Mar 22 '23 at 16:23
  • I would prefer not to use unmaintained libs in general, I think they are more risky. Potential bugs and vulnerabilities are not fixed, unless you fix them yourself by forking it. Of course, some small libs that are stable enough may not need updates, but I don't think this is the case for numeral.js – DLight Mar 23 '23 at 13:40
  • @DLight "stable enough" is subjective, cheers – am05mhz Mar 24 '23 at 08:54