Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
New Developer
JavaMEUser
Posts: 55
Registered: 04-07-2009
Accepted Solution

Math.round() and Math.random()

Hi,

 

Do we have equivalent methods in J2ME for  Math.round() and Math.random()? I could not find it either in Java.util.Math or net.rim.device.api.Math

 

Thanks.

Reshma

Please use plain text.
Developer
peter_strange
Posts: 14,611
Registered: 07-14-2008

Re: Math.round() and Math.random()

Don;t know of a round function.  For random, see Random class.

Please use plain text.
Developer
pwerry
Posts: 177
Registered: 01-21-2009
My Carrier: Vodafone

Re: Math.round() and Math.random()

[ Edited ]
You can use fixed point math found in net.rim.device.api.math.Fixed32 to obtain a rounded integer if you are rounding after integer computations. If you are trying to round a float, use net.rim.device.api.util.MathUtilities.round( float )
Message Edited by pwerry on 04-07-2009 04:50 PM
Please use plain text.
New Developer
JavaMEUser
Posts: 55
Registered: 04-07-2009

Re: Math.round() and Math.random()

[ Edited ]

Thanks for your response.

 

I am trying to round a float but I did NOT find a method in MathUtilities to round a float.  I found that MathUtilities.round(float) was a new method added in JDE 4.6.  I am using JDE 4.2.1.  Is there any other way ? 

 

Message Edited by JavaMEUser on 04-09-2009 12:52 AM
Please use plain text.
Developer
BBDeveloper
Posts: 3,951
Registered: 07-15-2008

Re: Math.round() and Math.random()

MathUtilities.round(float) is available only in 4.6.0 and later versions.

Use Search. "Accept Solution" If the problem is resolved.
Please use plain text.
Developer
mantaker
Posts: 1,477
Registered: 12-30-2008

Re: Math.round() and Math.random()

[ Edited ]

@ JavaMEUser, You can write your own Math class for your application, use the following code..

 

 

public static int round(float f)
{
return (int)(f + 0.5F);
}
public static long round(double d)
{
return (long)(d + 0.5D);
}


Thanks!
Message Edited by mantaker on 04-09-2009 10:58 AM
--
Manimaran Selvan
Co-Founder, Tech Lead,
Equity Markets Research Group
Please use plain text.
Developer
webmasterpdx
Posts: 558
Registered: 11-25-2008

Re: Math.round() and Math.random()

I have a library of fast math functions I was creating and these are the existing libs I found with math functions in 4.7. There are quite a bit....

 

 *     java.lang                                  Has a bunch of types like Float, etc, that have some useful functions.

 *      java.lang.Math                          Normal Java math functions. This is lacking some.
 *      net.rim.device.api.util.MathUtilities   Missing Math functions in double precision.
 *      net.rim.device.api.math               Blackberry math (The ones below are part of that)

 *      Fixed32                                    16.16 fixed point math

 *      VecMath                                  Uses Fixed32 to transform graphics.

 *      java.util                                    Has Random and a few other useful ones.

 

I've used Fixed32 and VecMath a lot for polygon manipulation.

 

Round exists from double precision to long (64-bit integer) in MathUtilities.

 

Random exists under java.util

 

Whenever you need a function or class, go to the javadocs, select all classes, then at the top, click on Index, wait for it to load completely. Then click on the letter that your function starts with (like R for random). Then scroll down until you find it. Usually works quite well.

 

Enjoy

 

-D

===============

Donald Murray

CTO IPPUB LLC

 

"If this solves your problem, click on Solved and Kudos for the person who helped you"

 

Please use plain text.