Created
November 1, 2012 19:40
-
-
Save pedrotoliveira/3995946 to your computer and use it in GitHub Desktop.
Função de format moeda brasileira
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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