1
I am creating a function for when the user type in a EditText
, he returns me with a masca in the field,
Example:
- Typing 1 returns "R$ 0.01"
- When typing 11 returns "R$ 0.11" and so on and so forth
I tried this way
public static String addMask(final String textoAFormatar) { String formatado = ""; int i = 0; if (textoAFormatar.length() == 1) { String mask = "R$ #.##"; DecimalFormat format = new DecimalFormat("0.00"); String texto = format.format(textoAFormatar); // vamos iterar a mascara, para descobrir quais caracteres vamos adicionar e quando... for (char m : mask.toCharArray()) { if (m != '#') { // se não for um #, vamos colocar o caracter informado na máscara formatado += m; continue; } // Senão colocamos o valor que será formatado try { formatado += texto.charAt(i); } catch (Exception e) { break; } i++; } } return formatado; }
Take a look in that library, I use it and it works right for this problem.
– Grupo CDS Informática
@Rogersmarques, please read the guides for OS beginners, especially the topic on the comments, at the point when one should or should not make comments.
– Grupo CDS Informática
@Rogersmarques , Starkoverflow serves for this, looking for professionals to help us with our beginner problems
– Jonnys J.
@Grupocdsinformática perfect, worked perfect in the project !!!!!
– Jonnys J.