0

I'm a newbie on angularJS.

I want to divide 2 numbers, but I want to round the result with two digits.

I try many things :

<input type="number" ng-model="var3" ng-value="var3 = Math.round((var1/var2)*100)/100" />

<input type="number" ng-model="var3" ng-value="var3 = (var1/var2).toFixed(2)" />

Do you have any ideas ?

JsFiddle

4
  • 1
    if var3 is always equal to var1/var2 why do you want to have input for it? just display it as text Commented Dec 3, 2015 at 13:08
  • I display it because the user want to see the value before submitting form. Commented Dec 3, 2015 at 13:16
  • 1
    Your toFixed() example would work with 1 minor change, Angular returns a string, change the input type to text. I would also suggest changing the input to disabled that way the user knows it will be submitted but they aren't allowed to change the value. Commented Dec 3, 2015 at 13:33
  • That's awesome Asok. Very big thanks, I can keep my ng-model and ng-value. Thank you so much ! Commented Dec 3, 2015 at 13:45

1 Answer 1

1

Not sure how to do it with ng-model and ng-value but this filter works fine:

<input type="number" value="{{ var3=var1/var2 | number:2 }}" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. It works. I don't know why it does'nt work with ng-value or ng-model. I will manage with you answer to do that I want. Thanks !

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.