Skip to content

Instantly share code, notes, and snippets.

@pedrotoliveira
Created November 1, 2012 19:40
Show Gist options
  • Save pedrotoliveira/3995946 to your computer and use it in GitHub Desktop.
Save pedrotoliveira/3995946 to your computer and use it in GitHub Desktop.
Função de format moeda brasileira
public final class CurrencyFormat {
public static String format(String valor) {
Currency curr = Currency.getInstance(new Locale("PT", "br"));
char[] reversed = new StringBuilder(valor).reverse().toString().toCharArray();
StringBuilder sb = new StringBuilder();
int index = 0;
for (char c : reversed) {
if (index == curr.getDefaultFractionDigits()) {
sb.append(",");
} else if (index == curr.getDefaultFractionDigits() + 3) {
sb.append(".");
index = curr.getDefaultFractionDigits();
}
sb.append(c);
index++;
}
return curr.getSymbol() + " " + sb.reverse();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment