Created
February 24, 2015 01:43
-
-
Save tommyettinger/69f167b337f40c72d591 to your computer and use it in GitHub Desktop.
MiniFormatter: formats some printf-style format strings on GWT
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
public class AndroidMiniFormatter implements MiniFormatter { | |
public static String format(final String formatString, final Object... args) { | |
return String.format(formatString, args); | |
} | |
} |
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
public class DesktopMiniFormatter implements MiniFormatter { | |
public static String format(final String formatString, final Object... args) { | |
return String.format(formatString, args); | |
} | |
} |
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 com.google.gwt.i18n.client.NumberFormat; | |
public class HtmlMiniFormatter implements MiniFormatter { | |
private static NumberFormat fmt_d = NumberFormat.getFormat("############"), | |
fmt_f = NumberFormat.getFormat("#######.#####0"), | |
fmt_f1 = NumberFormat.getFormat("#######.0"), | |
fmt_f2 = NumberFormat.getFormat("#######.#0"); | |
@Override | |
public static String format(final String formatString, final Object... args) | |
{ | |
string msg = formatString; | |
char[] cs = msg.toCharArray(); | |
int ctr = 0; | |
for (int pos = 0; pos < cs.length() - 1; ++pos) | |
{ | |
if(cs[pos] == '%') | |
{ | |
char next = cs[++pos]; | |
switch(next) | |
{ | |
case 'f': msg = msg.replaceFirst("%f", fmt_f.format((java.lang.Number)(args[ctr++]))); | |
break; | |
case 'd': msg = msg.replaceFirst("%d", fmt_d.format((java.lang.Number)(args[ctr++]))); | |
break; | |
case 's': msg = msg.replaceFirst("%s", args[ctr++].toString()); | |
break; | |
case '.': if(cs[++pos] == '1') | |
{ | |
msg = msg.replaceFirst("%.1f", fmt_f1.format((java.lang.Number)(args[ctr++]))); | |
pos++; | |
} | |
else if(cs[++pos] == '2') | |
{ | |
msg = msg.replaceFirst("%.2f", fmt_f1.format((java.lang.Number)(args[ctr++]))); | |
pos++; | |
} | |
break; | |
} | |
} | |
} | |
return msg; | |
} | |
} | |
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
public interface MiniFormatter { | |
public static String format(final String formatString, final Object... args) { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment