Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Heavy bug with german number formatting #756

Closed
diquadrat opened this issue Dec 21, 2022 · 1 comment
Closed

Heavy bug with german number formatting #756

diquadrat opened this issue Dec 21, 2022 · 1 comment

Comments

@diquadrat
Copy link

diquadrat commented Dec 21, 2022

Current behavior

German numbers with on grouping/thousands dot will be handled on page-load (or ajax-replace) as english numbers
so 1.000 gets 1.0 (one thousand is converted to one)
900.000 gets 900
(only if there are no decimal digits)

Expected behavior

German numbers between 1.000 und 999.999 should not be changed

Steps to reproduce the problem

Save and open following HTML:

<html>
   <head>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/autonumeric/4.6.0/autoNumeric.js"></script>
      <script>
         window.addEventListener("load", (event) => {
            new AutoNumeric("input", {
               currencySymbol: "",
               decimalCharacter: ",",
               digitGroupSeparator: ".",
               decimalPlaces: 2
            });
         });
      </script>
   </head>
   <body>
      <input type="text" value="12.345" />value=12.345 - the german 12.345,00 but without decimal places<br />
      <input type="text" value="1.234.567"/>value=1.234.567 - the german 1.234.567,00 but without decimal places<br />
   </body>
</html>

https://pastebin.com/BdgYVacY

Fix

<Also, when creating an extract of your problem, you are likely to find the cause of it, and fix it yourself, 🎉 ;)>

The Problem is in autoNumeric.js (4.6.0) line 10389 in the "_toNumericValue" function.

The first IF for "normal" (english-styled) numbers also miss-matches for german integers between 1.000 and 999.999.
My fix was to check the actual decimalCharacter by changing the original
if (AutoNumericHelper__WEBPACK_IMPORTED_MODULE_0_["default"].isNumber(Number(value))) {
to
if (settings.decimalCharacter == this.getDefaultConfig().decimalCharacter && AutoNumericHelper__WEBPACK_IMPORTED_MODULE_0_["default"].isNumber(Number(value))) {

(found no static access to AutoNumeric__WEBPACK_IMPORTED_MODULE_0_["default"].options.decimalCharacter.dot)

Another question:

Is there a "hidden" (i haven't found it) function to add the decimal digits directly while typing, not afterwards on blur?
So when i type "1" in an empty field it gets automatically to "1.00" (in english-format), with cursor staying after "1"?

@AlexandreBonneau
Copy link
Member

Here is the link to the related live code example to try to reproduce the aforementioned problem.

AutoNumeric does try to parse any javascript-formatted Number passed as the 'default' value.
AutoNumeric does not try to unformat the default attribute value, so using 12.345, it's correctly interpreted as the decimal number 12.345.

As you can see in the code, the docs for _toNumericValue() states:

Convert the `value` parameter that can either be :
- a real number,
- a number represented in the scientific notation (i.e. -123.4567e-6)
- a string representing a real number, or
- a string representing a localized number (with specific group separators and decimal character),
...to a string representing a real 'javascript' number (i.e. '1234' or '1234.567').

This function returns `NaN` if such conversion fails.

All in all, the default attribute value should always be used with a Javascript Number, not with a formatted one.

This is a feature, not a bug :)

@AlexandreBonneau AlexandreBonneau closed this as not planned Won't fix, can't repro, duplicate, stale Mar 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants