Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in Angular Entering Decimal Number in german format "having comma" is not work well

I am trying to localize the data entry Module (written in angular) based on the user PC (example in case the PC language setting is German ) so the module should accept the "," as a decimal separator instead of "." , So I have used the Angular localization files (e.g.: angular-locale_de-de.js ) However I have an issue when insert the decimal number using the comma as the following : in German PC settings, when the users insert the decimal number at this format "5,4" at the input Text Box, the display at the Label to this value shows Empty instead of 5,40 the Label at the code as the following : <label>{{mynumber | number:2}}</label>

but when the user insert : "5.4" the label show "5,40" for simplicity I am using the code below

<html>
   <head>
   <script LANGUAGE=javascript src="angular.js"></script>
       <script LANGUAGE=javascript src="https://code.angularjs.org/1.2.9/i18n/angular-locale_de-de.js"></script>
      </script>
</head>
<body ng-app  >
<input  type=number id="txt" ng-model="mynumber" >
<label>{{mynumber| number : 2}}</label>
</body>
</html>

And that also applied when using the filter below at the controller : $filter('number')(mynumber, 2) can you please help on how correctly format the number when insert number using comma separator (like above should be 5,40 instead of empty) ? Please note : this problem is exist even using "angular-locale_de-de.js"

Also can you please advice on how can I load the right angular localization file (e.g.: angular-locale_de-de.js depending of the User Language PC settings )? Thanks

like image 883
JPNN Avatar asked Sep 17 '25 13:09

JPNN


1 Answers

There is angular directive (I am the author) which allow only numbers, minus sign and selected separator ',' or ','. Link: https://github.com/uhlryk/angular-dynamic-number

You can config number of integer digits, number of fraction digits, choose separator, or allow positive, negative or both numbers. It block other chars in input view value, but dynamically convert model value. This module also has filter to modify ngBind to show for example comma separator instead of dot.

After Installation (bower, npm or manualy) add module to your application as dependency, and then:

<input  type="text" id="txt" ng-model="mynumber" awnum num-sep="," num-fract="2">
<label>{{mynumber| awnum : 2 : ',' : 'round' : 'true'}}</label>

I build this module to process forms with prices in Poland.

like image 181
Krzysztof Sztompka Avatar answered Sep 23 '25 06:09

Krzysztof Sztompka