Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello. I want to limit Decimals values to 2 Digits in angular js code.

Could someone can help me out.

Example: 1.1245562 should limit to 1.12 , Should not allow to enter more than 2 Digits after decimals ?

What I have tried:

i have used ng-pattern to limit, But it not work
Posted
Updated 18-Apr-18 19:29pm
v2
Comments
Patrice T 17-Apr-18 6:21am    
Show what you have done.
Member 11520335 17-Apr-18 6:41am    
$scope.regex = '/\d{0,8}(\.\d{0,4})?/'; //I have used this to limit Decimals to 4 and Integer part to 8

.run(['$templateCache', function($templateCache) {
$templateCache.put('settingControlTemplate',
'' +
'<input type="text" ng-model="settingParam.activeValue" ' +
ng-pattern ="regex" ng-focus>' +
'');
}]
Patrice T 17-Apr-18 7:19am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

1 solution

You can use "number" filter. Example for 2 decimal places is below.
{{val | number:2}}
 
Share this answer
 
Comments
Member 11520335 17-Apr-18 7:48am    
No this also i have tried . Did not work.
Member 11520335 19-Apr-18 1:29am    
Is there anything other than this ?
Sanjay K. Gupta 19-Apr-18 3:35am    
It should work. https://plnkr.co/edit/QU4r05n9rQprwyL9Ltxh
Sanjay K. Gupta 19-Apr-18 3:32am    
What is type of "val"? If it other that float then convert it to first in float using parseFloat method.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900