calum.org:~#

J2ME functions that aren't included - but should be

Tags: programming, java, j2me, rants,

Added: 2007-02-28T15:38

J2ME functions that aren't included - but should be

Because J2ME is annoying, it doesn't have a function that rounds a number, say 1234.56789 to a certain number of decimal places. So here is mine.

PS. I am learning Java, so if it doesn't adhere to the One True Java Way (tm), please feel free to write me a long, vitriolic diatribe criticising me and delete it before sending. Otherwise, leave a comment telling me where I went wrong.


public static double calumround(double arg, int places) {
double tmp = (double) arg * (pow(10,places));
int tmp1 = (int)Math.floor( tmp + 0.5 );
double tmp2 = (double) tmp1 / (pow(10,places));
return tmp2;
}


Because J2ME is still annoying, it also doesn't have a "pow" function. So here is mine.


public static int pow(int arg, int times){
int ret = 1;
for ( int i = 1 ; i <= times ; i++ ) {
ret = ret * arg;
}
return ret;
}


These functions work well enough for me and what I want from them. If you're planning to use them for landing a multi-million dollar probe on a remote planet, you might want to check them out first. Or not - it's your call.

J2ME doesn't have a lot of stuff that I consider necessary. Which is annoying. However, when I saw my colleague trying to learn Symbian, I decided that Java was probably the easier evil of the two.

posted by Calum on 2007-02-28T15:38 under

Comments

Comment

wow...the second works fine!!! How do i call the method, say i already have "double Weight" computed and need to be aproximated to two decimal places and it appears in different functions...

Comment

double rounded = calumround(3.141592654, 3); will round PI to 3 decimal places.
Add a comment

Your IP:
Please enter 9072630 here: