adamwdraper / Numeral-js

A javascript library for formatting and manipulating numbers.
http://numeraljs.com
MIT License
9.66k stars 930 forks source link

German number representation is incorrect #139

Open asagrawal opened 10 years ago

asagrawal commented 10 years ago

Hi,

I am considering using Numeral.js and it's language extensions in my project. I noticed that the German number representation is incorrect.

For example: A number "12345678.91" when formatted using DE language pack, is incorrectly formatted as "12 345 678,91", whereas it should ideally be "12.345.678,91" per http://userguide.icu-project.org/formatparse

Am i missing something here? Here's my sample code - require(['lib/numeral/min/numeral.min/numeral', 'lib/numeral/languages/de' ], function (numeral, language) { numeral.language('de', language); var number = numeral(12345678.91); console.log("DE:" + number.format('0,0.00')); });

Thanks for creating Numeral.js and it's associated language packs!

asagrawal commented 10 years ago

It seems that the bug is because the de language "delimiters" are setup as: delimiters: { thousands: ' ', decimal: ',' }

The thousands part should be a "." and not a " " from what i understand. Any thoughts?

morioust commented 10 years ago

Yes, the thousands delimiter should be a '.', not a ' '.

piefel commented 10 years ago

Actually, no, the space is not incorrect. You can use both in German typograhpy.

morioust commented 10 years ago

OK, I agree. The DIN 1333 standard does specify both. I was actually surprised that it favours the space over the dot thousands delimiter. I personally feel that I rarely see a number separated by spaces. It does allow the dot for currency formatting. The dot is however frequently used in systems (including many spreadsheet programs, etc). Wikipedia has a short note (German) on some of the benefits and issues with using a dot as a delimiter.

There's a separate technical issue with the space-delimiter -- it frequently causes line breaks in long numbers, which should not happen either. There are two alternatives to fix this issue:

  1. Use the narrow no-break space character ( )
  2. Specify white-space: nowrap in CSS

While I still prefer the dot as a thousands delimiter over a space, I do see a valid point in sticking with the space-delimiter. In this case it might make sense to switch to the narrow no-break space character though to prevent line breaks.

piefel commented 10 years ago

The breaking issue is real. Moreover, narrow spaces are not necessarily any narrower in the actual rendering. The dot is, in fact, often a better-looking choice. Ideally, this would be configurable.

morioust commented 10 years ago

As far as I can tell, the options regarding the thousands-delimiter in the German language implementation are as follows:

  1. Decide on language-level: Pick a single formatting option a) No change: keep current white-space separator b) Use NNBSP c) Use dot (.) - merge #192 => I'd chose the dot due to subjective perception of frequency of usage. The closest to the standard is probably NNBSP. Probably none of these options will make everyone happy.
  2. Decide on project-level: Everyone overrides the German language set with project-specific custom language, if desired.
  3. Decide on library-level: a) Make Numeral.js agnostic and provide some sort of configurability. b) Make Numeral.js incredibly complex and understand the intrinsic rules of all languages (e.g. in this case, use a narrow space for all numbers, use dot for currency-formatting).

In general, Option 1.b is probably better than the current implementation of 1.a, due to line-breaks. Personally I still prefer 1.c. Option 3 sounds good, but is probably unrealistic. Especially 3.b sounds wholly unattainable, since there are likely conflicting standards. So you'd need 3b + 3a. I'm skeptical...

crebuh commented 9 years ago

Is there a possibility to overwrite the default delimitier? I dont want to modifiy the language file, because if I update the library via bower the modifications are gone.

akkie commented 9 years ago

@crebuh You could override the delimeter with jQuery $.extend or any other extend method.

numeral.language('de', $.extend(numeralDE, { delimiters: {thousands: '.', decimal: ','} }));
buffcode commented 7 years ago

Couldn't we just add a de-alt.js with dot configuration. This would save many german developers lots of time. As both variants are standardized, both options should be available.

jprivillaso commented 5 years ago

Anyone that may be still struggling with it, I managed to make it work as follows:

import numeral from 'numeral';
require('numeral/locales/de');

numeral.locale('de');
numeral.localeData('de').delimiters.thousands = '.';

export const formatNumber = (number) => (
  numeral(number).format('0,0.000')
);

And then I call my function like

formatNumber(12302) // 12.302
formatNumber(12302,34) // 12.302,340