Total Pageviews
Showing posts with label gwt. Show all posts
Showing posts with label gwt. Show all posts
Monday, February 13, 2012
Wednesday, November 16, 2011
Convert byte size into human readable format in Java ( and GWT )
Java:
Orgin from here
public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit) return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i");
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
Java GWT:public static String humanReadableByteCount(long bytes, boolean si) {
int unit = si ? 1000 : 1024;
if (bytes < unit)
return bytes + " B";
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp - 1)
+ (si ? "" : "i");
return toFixed(bytes / Math.pow(unit, exp), 1) + " " + pre
+ "B";
}
public static native String toFixed(double d, int fixed)/*-{
return d.toFixed(fixed);
}-*/;
Orgin from here
JSNI function in GWT to format numbers
public class NumberUtil {
public static native String toFixed(double d, int fixed)/*-{
return d.toFixed(fixed);
}-*/;
public static native String toPrecision(double d, int precision)/*-{
return d.toPrecision(precision);
}-*/;
}
see also this article.
Format numbers in javascript
var num = 1; var result = num.toFixed(2); // result = 1.00 num = 123.4567; result = num.toFixed(3); // result = 123.456 num = 123.4567; result = num.toPrecision(4); // result = 123.4 num = 123.4567; result = num.toPrecision(3); // result = 123 num = 555.55; result = num.toPrecision(2); // result will equal 5.6e+2
see also this article.
Sunday, November 13, 2011
GWT: JSNI function to validate URL
public native boolean isValidUrl(String url) /*-{
var pattern = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
return pattern.test(url);
}-*/;
Subscribe to:
Posts (Atom)