1

I am developing an application in Ionic and use an input form field for my calculations.

<input type="number" ng-model="data" step="0.01"> 

However if I update the data with the value 100.10 the input box shows 100.1

I want to format the value in the input field so it always shows 2 decimals (100.10).

I have tried using filters, but they only work when I change the input to type "text". I want to use a type="number" because this only shows the numeric keyboard.

I have also tried:

parseFloat(""+data.toFixed(2)) 

but this also shows 100.1

2 Answers 2

0

Since you're using Ionic, you get to use Angular. And in Angular you can do this simply like this:

{{ number_expression | number : fractionSize}}

So, if in your case you have a variable dataand you would do it like this:

{{data | number : 2}}

More information in the official docs.

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

3 Comments

This does not work. This is for labels and normal text. I want the text in the actual input field to show 100.10
What browser are you using?
And also - a plunker or CodePen with the exact example would help us help you.
0

This is because the type of the input, which is number. If you change it to text will solve the problem.

Or just remove the parseFloat from data.toFixed(2). This way it will go as text to the form, with the trailing 0.

1 Comment

'I have tried using filters, but they only work when I change the input to type "text". I want to use a type="number" because this only shows the numeric keyboard.'

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.