1

am getting values from database as 10.00,520.00 but when am binding to input it showing as 10 and 520

example

<input type="text" ng-model="Amount">

  $scope.Amount =10.00 ;

value binding to text box as 10 but i want bind 10.00

1
  • if your amount is in integer then you have to convert it into string before you can show the value like you want to because you have taken input type text here Commented Aug 8, 2017 at 10:58

2 Answers 2

4

Check the below working code.

var app = angular.module('plunker', []);
    app.directive('validNumber', function () {
        return {
            link: function (scope, element, attrs, ngModelCtrl) {
                setTimeout(function () {
                    var digits = element.val().split('.')[1];
                    digits = digits == null ? 2 : digits.length;
                    element.val(parseFloat(element.val()).toFixed(digits));
                }, 100);
            }
        };
    })
    app.controller('MainCtrl', function ($scope) {
        $scope.Amount = 10.0001;

    });
<script data-require="[email protected]" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.11/angular.min.js" data-semver="1.5.11"></script>
<div ng-app="plunker" ng-controller="MainCtrl">
    <input type="text" ng-model="Amount" valid-number />
    {{Amount}}
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

good solution but if data from the server is coming upto 3 places, then your code wont work becuase you have hard coded upto two places in the directive.
Thanks @Gaurav! I will update it to handle this case.
1

this seems like what you need

<input type="number" step="0.01" value="0.001"/>

Reference: input 2 decimal places

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.