I'm trying to write an app that helps you manage your finances. I'm using an EditText
Field where the user can specify an amount of money.
I set the inputType
to numberDecimal
which works fine, except that this allows people to enter numbers such as 123.122
which is not perfect for money.
Is there a way to limit the number of characters after the decimal point to two?
This implementation of
InputFilter
solves the problem.I really liked Pinhassi's answer, but noticed that after the user had entered the specified number digits after the decimal point you could no longer enter text to the left side of the decimal point. The problem was that the solution only tested the previous text that had been entered, not the current text being entered. So here is my solution that inserts the new character into the original text for validation.
And the call to set the editText filter
More elegant way would be using a regular expression ( regex ) as follows:
To use it do:
Here is my solution:
If the user enters a number with more than two numbers after the decimal point, it will be automatically corrected.
I hope I have helped!
The simplest way to achieve that is:
I achieved this with the help of
TextWatcher
by the following wayThis solution will not allow the user to enter more than two digit after decimal point. Also you can enter any number of digits before decimal point. See this blog http://v4all123.blogspot.com/2013/05/set-limit-for-fraction-in-decimal.html to set filter for multiple EditText. I hope this will help. Thank you.