Created
August 3, 2010 04:33
-
-
Save aanoaa/505831 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.*; | |
import java.util.regex.*; | |
public class CurrencyRegex { | |
public static void main(String[] args) { | |
for (String arg : args) { | |
System.out.println(toCurrency(arg)); | |
} | |
} | |
public static String toCurrency(String str_num) { | |
Pattern p = Pattern.compile("(?<!\\.\\d)(?<=\\d)(?=(?:\\d\\d\\d)+(?!\\d))"); | |
Matcher m = p.matcher(str_num); | |
StringBuffer sb = new StringBuffer(); | |
while(m.find()) { | |
m.appendReplacement(sb, ","); | |
} | |
m.appendTail(sb); | |
return sb.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment