AngularJS decimal input: change comma to dot

dhrm

I'm using FCSA Number to handle decimal input in AngularJS.

If I use the maxDecimals option it almost works as expected. For two decimals, inputs like "100.333" are transformed to "100.33".

In my region comma is used as decimal separator, but the input on my website should use dot as decimal separator -- like this plugin does. That's fine. However, I would like that input like "100,33" are converted to "100.33".

How can I do that?

Manube

I am afraid I am unfamiliar with FCSA number :(

However, if your goal is to understand how to implement a simple, angular-oriented solution that may well suit your needs, read on... You will find below the price directive I implemented in a plunker. It is quite straightforward, and I recommend you take the time to study it, then implement your own solution, inspired by both the price directive and the FCSA source code.


  1. a filter to convert comma numbers to decimal numbers:

    app.filter('comma2decimal', [
    function() { // should be altered to suit your needs
        return function(input) {
        var ret=(input)?input.toString().trim().replace(",","."):null;
            return parseFloat(ret);
        };
    }]);
    

This filter will automatically convert data from view format (with a comma) to model format (your scope, with a decimal).


  1. a filter to convert decimal numbers to comma numbers:

    app.filter('decimal2comma', [
    function() {// should be altered to suit your needs
        return function(input) {
            var ret=(input)?input.toString().replace(".",","):null;
            if(ret){
                var decArr=ret.split(",");
                if(decArr.length>1){
                    var dec=decArr[1].length;
                    if(dec===1){ret+="0";}
                }//this is to show prices like 12,20 and not 12,2
            }
            return ret;
        };
    }]);
    

This filter will automatically convert data from model format (your scope, with a decimal) to view format (your view, with a comma).


  1. a directive named price that uses those two filters:

    app.directive('price', ['$filter',
    function($filter) {
    return {
        restrict:'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ngModelController) {
            ngModelController.$parsers.push(function(data) {
                //convert data from view format to model format
    
                data=$filter('comma2decimal')(data);
    
                return data;
            });
    
            ngModelController.$formatters.push(function(data) {
                //convert data from model format to view format
    
                data=$filter('decimal2comma')(data);
    
                return data;
            });
        }
    };}]);
    

See this working plunker, showing how everything works together.

Initially, a number (e.g. coming from database) has a decimal value in controller scope ($scope.price=25.36;);

In the view, it appears like: 25,36 in the input, and 25.36, as in the database, below.

Now enter any comma number: it is automatically converted to decimal number for insertion in database.

Hoping this answers your question.


Advantage of using a directive: it is reusable, everywhere you need it on your website.

Advantage of using two filters: separation of concerns.


This approach lets users use numbers they are most used to in your views. However, the numbers entered can be altered in the background before insertion into database, so they are formatted properly.

And when you fetch prices/numbers from the database, they are automatically altered, before showing in the view in a way better suited to the reader.

If you need other options like in the FCSA Number directive, you can easily add them in your filters.

You will probably need custom functions and model validation, using ngModelCtrl.$setValidity.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Number input replacing decimal comma with dot

From Dev

How to change the decimal separator from a comma to a dot in SQL Server

From Dev

AngularJS dot to comma

From Dev

Change dot to comma at DecimalFormat

From Dev

Loop to convert decimal comma (,) into dot (.) to change class of data.frame columns

From Dev

Is is possible to change one regional setting for Wine applications - use decimal dot instead of comma for numbers?

From Dev

Replace comma with dot in a input Javascript

From Dev

Replace comma with dot in a input Javascript

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

Difference between decimal number with comma and dot Javascript

From Dev

Allow dot and comma in numbers, not only for decimal

From Dev

String to decimal conversion: dot separation instead of comma

From Dev

Regex for decimal number dot instead of comma (.NET)

From Dev

Regex for formatting comma for decimal places and dot for thousands

From Dev

NumericUpDown: accept both comma and dot as decimal separator

From Dev

C# aspx decimal thousand with comma and decimal with dot

From Dev

How to change decimal comma to decimal period in numpad?

From Dev

What determines if input values are output with a dot or a comma?

From Dev

How to accept both dot and comma as a decimal separator with WTForms?

From Dev

If decimal value, convert to two decimals AND dot separated value to comma separated

From Dev

Decimal pad can't do math with comma. Need dot

From Dev

Magento: Decimal Price in spanish language display Dot instead comma

From Dev

Formatting doubles to two decimal places in Java produces a comma instead of a dot

From Dev

Values over plot– one decimal place, a comma instead of a dot

From Dev

Replace the decimal dot with a comma on an axis in D3?

From Dev

How to convert floating point decimal separator from dot to comma in Javascript

From Dev

Handling decimal separators (comma and dot) for a whole program with few lines of code

From Dev

Swift: Show $ and comma in the decimal textfield input

From Dev

Replace ,(comma) by .(dot) and .(dot) by ,(comma)

Related Related

  1. 1

    Number input replacing decimal comma with dot

  2. 2

    How to change the decimal separator from a comma to a dot in SQL Server

  3. 3

    AngularJS dot to comma

  4. 4

    Change dot to comma at DecimalFormat

  5. 5

    Loop to convert decimal comma (,) into dot (.) to change class of data.frame columns

  6. 6

    Is is possible to change one regional setting for Wine applications - use decimal dot instead of comma for numbers?

  7. 7

    Replace comma with dot in a input Javascript

  8. 8

    Replace comma with dot in a input Javascript

  9. 9

    NumericUpDown: accept both comma and dot as decimal separator

  10. 10

    Difference between decimal number with comma and dot Javascript

  11. 11

    Allow dot and comma in numbers, not only for decimal

  12. 12

    String to decimal conversion: dot separation instead of comma

  13. 13

    Regex for decimal number dot instead of comma (.NET)

  14. 14

    Regex for formatting comma for decimal places and dot for thousands

  15. 15

    NumericUpDown: accept both comma and dot as decimal separator

  16. 16

    C# aspx decimal thousand with comma and decimal with dot

  17. 17

    How to change decimal comma to decimal period in numpad?

  18. 18

    What determines if input values are output with a dot or a comma?

  19. 19

    How to accept both dot and comma as a decimal separator with WTForms?

  20. 20

    If decimal value, convert to two decimals AND dot separated value to comma separated

  21. 21

    Decimal pad can't do math with comma. Need dot

  22. 22

    Magento: Decimal Price in spanish language display Dot instead comma

  23. 23

    Formatting doubles to two decimal places in Java produces a comma instead of a dot

  24. 24

    Values over plot– one decimal place, a comma instead of a dot

  25. 25

    Replace the decimal dot with a comma on an axis in D3?

  26. 26

    How to convert floating point decimal separator from dot to comma in Javascript

  27. 27

    Handling decimal separators (comma and dot) for a whole program with few lines of code

  28. 28

    Swift: Show $ and comma in the decimal textfield input

  29. 29

    Replace ,(comma) by .(dot) and .(dot) by ,(comma)

HotTag

Archive