Created
April 25, 2014 09:18
-
-
Save gnosis23/11283239 to your computer and use it in GitHub Desktop.
Money converter
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 static String formatFen2Yuan2(long amt) { | |
boolean neg = false; | |
if(amt < 0) { | |
neg = true; | |
amt = -amt; | |
} | |
long left = amt / 100L; | |
long right = amt % 100L; | |
StringBuilder sb = new StringBuilder(); | |
if(neg) { | |
sb.append("-"); | |
} | |
sb.append(NumberFormat.getNumberInstance(Locale.US).format(left)); | |
sb.append("."); | |
if(right < 10) { | |
append0(sb, 1); | |
} | |
sb.append(right); | |
return sb.toString(); | |
} | |
public static long formatYuan2Fen2(String yuan) { | |
return rightMovePoint(yuan, 2); | |
} | |
// public static long yuan2Li(String yuan) { | |
// return rightMovePoint(yuan, 3); | |
// } | |
private static long rightMovePoint(String origin, int offset) { | |
if(StringUtils.isEmpty(origin)) | |
return 0; | |
String t = origin.replace(",", ""); | |
StringBuilder sb = new StringBuilder(t); | |
int pntIndex = sb.indexOf("."); | |
int len = sb.length(); | |
if(pntIndex == -1) { | |
append0(sb, offset); | |
} | |
else if(len - pntIndex > offset) { | |
sb.delete(pntIndex + offset + 1, len); | |
sb.deleteCharAt(pntIndex); | |
} | |
else { | |
append0(sb, offset + 1 + pntIndex - len); | |
sb.deleteCharAt(pntIndex); | |
} | |
return Long.valueOf(sb.toString()); | |
} | |
private static StringBuilder append0(StringBuilder sb, int count) { | |
for(int i = 0; i < count; i++) | |
sb.append("0"); | |
return sb; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment