J2ME - number format
I need to format decimal numbers with patterns in J2ME just like java DecimalFormat.
I see an option to 开发者_如何学JAVAport Format/NumberFormat/DecimalFormat.
Can you suggest ready to use implementation or library?
Thanks!
So, I've manage to port desktop java implementation: bbnumberformat:
String[] patterns = new String[] { "#,#00.00#", "0.0;(0.0)",
"0.###E0" };
DecimalFormat format = (DecimalFormat) DecimalFormat
.getNumberInstance();
double value = -12.321;
for (int i = 0; i < patterns.length; i++) {
String pattern = patterns[i];
format.applyPattern(pattern);
String text = "Pattern: " + pattern
+ " Sample: "+format.format(value)+"\n";
add(new LabelField(text));
}
alt text http://img220.imageshack.us/img220/6245/9000.jpg
The code below is method that shows how to round a double to n decimals:
private double round(double num,int numDecim){
long p=1;
//next line – calculate pow(10,brDecim)
for(int i=0; i<numDecim; i++)p*=10;
return (double)(int)(p * num + 0.5) / p;
}
You may use double p=1.0;
or p*=10.0;
instead.
精彩评论