How to Round Floating point number (Double) to 2 Decimal Points in J2ME JavaME
August 24th, 2007 Posted in J2ME / JavaME, newbie, Tips, Tricks, Guide
The minimalistic design of JavaME CLDC means that a lot of packages from the standard Java has to be trimmed out and this includes the flexible NumberFormat packages which is useful for formatting numbers in Java.
The absence of NumberFormat and java.text package means that you can’t even round a floating point number!
However there is a workaround to this problem, with this simple method, you can round floating point number (Double) roughly to a specific number of decimal points.
//round to 2 decimal points double number = (double)(int)((bmi+0.005)*100.0)/100.0; //round to 4 decimal points double number = (double)(int)((bmi+0.00005)*10000.0)/10000.0;
The code above shows you how to round a double to 2 and 4 decimal points, you can change the number of zeros above to round it to your desired decimal points.
Hope that will help you in your JavaME / J2ME programming.
[tags]j2me,javame,midp,java,sdk,j2se[/tags]
October 17th, 2007 at 8:44 pm
thnx
January 6th, 2008 at 4:15 pm
thanks lots!
June 14th, 2008 at 7:54 am
excelent!!!, very very ingenious. You are a genious!! very thanks
July 23rd, 2008 at 2:52 am
thank u very much!
September 2nd, 2008 at 1:45 am
Thanx man
October 11th, 2008 at 8:32 am
Thanks
March 19th, 2009 at 1:46 pm
Awesome! Its a very small snippet but very useful. You saved my hell lot of time. Thanks man. You rocks.
May 19th, 2009 at 1:06 am
This is not rounding, is trunking.
22.92893723062 turns into 22.9288 and not 22.9289
47.026586582836 turns 47.0265 and not 47.0266
May 7th, 2010 at 10:53 am
DONT WORK
May 14th, 2010 at 9:39 pm
maaaaan you saved my day!!! really tks ;D
June 28th, 2010 at 8:35 pm
??? ???
thanks
August 11th, 2010 at 5:57 am
This will round not truncate. Math.Pow is my own pow function().
double sum = numberToRound;
double round = .5 / Math.Pow(10, precision+1);
sum = (sum > 0) ? sum + round : sum – round;
int temp=(int)((sum*Math.Pow(10,precision)));
sum = (((double)temp)/Math.Pow(10,precision));
October 27th, 2010 at 7:37 am
Skip Hop Studio Diaper Tote Tote is awesome.So many pockets – and also not really these modest, worthless pockets, either. All of the storage compartments tend to be a great size and truly assist keep every thing organized. The handles are a fantastic length and fit nicely over the actual shoulder; the tackle shoulder straps also stay place on my own arm, which is actually essential when you’re having a baby. The handbag looks wonderful, as well. Not as well fancy, but not as well casual. (I have it in black) The material is soft (can’t assume of a greater word) so it’s simple to squeeze in to tight spaces – but yet it is sturdy. I have a Fleurville Lexi carry and I love it, as well, but that bag is actually type of inflexible. I believe this Skip*Hop can turn into my everyday tote. It’s a bit big – so if you’re not in to large totes this may well be superior as an over-night baby diaper tote.
November 26th, 2010 at 11:40 am
You are amazing!!!
Thanks a lot
AC/DC Rules
XD
March 4th, 2011 at 5:05 pm
Round in Java ME, e.g. in J2ME
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.
March 11th, 2011 at 6:31 pm
Thank you soooo much!!!! Been working on this for ages!