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]

 

Random Posts


16 Responses to “How to Round Floating point number (Double) to 2 Decimal Points in J2ME JavaME”

  1. thanasis Says:

    thnx



  2. taro Says:

    thanks lots!



  3. pao Says:

    excelent!!!, very very ingenious. You are a genious!! very thanks



  4. ana Says:

    thank u very much!



  5. viv Says:

    Thanx man



  6. Eric Says:

    Thanks



  7. Sameer Nafdey Says:

    Awesome! Its a very small snippet but very useful. You saved my hell lot of time. Thanks man. You rocks.



  8. Léo Says:

    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



  9. Felipe Says:

    DONT WORK



  10. Ricardo Says:

    maaaaan you saved my day!!! really tks ;D



  11. armin Says:

    ??? ???

    thanks



  12. lark Says:

    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));



  13. Alexandra Pali Says:

    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.



  14. xD Says:

    You are amazing!!!
    Thanks a lot

    AC/DC Rules
    XD



  15. Ljubisa Cvetic Says:

    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.



  16. Grateful Guy Says:

    Thank you soooo much!!!! Been working on this for ages!



Leave a Reply

This blog is protected by dr Dave\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'s Spam Karma 2: 141686 Spams eaten and counting...