×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    87

    Rounding double value

    Hello, is there any round off function available in j2me. let's say if i want to get ronded value of float f = 3.45 and want to store it in integer data type then please tell me the method.

  2. #2
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Rounding double value

    Look at Math.round(). (I'm assuming that ME implements all of Math.) You have to first multiply the number, in your example, by 100.0, then do the round() call to get an int that is 100 times the float/double value.

    If Math.round() is for some reason unavailable you'd multiply by 100.0, add 0.5, and then take the int of that.

    A lot harder if you want to round it to a value with N decimal digits of precision, but with the decimal point allowed to float.

    http://java.sun.com/j2se/1.4.2/docs/...nd%28double%29

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: Rounding double value

    Here's round():
    Code:
        public static long round(double a) {
            return (long)floor(a + 0.5d);
        }
    (Of course, this just leaves you asking what floor() is, I suppose.)

  4. #4
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    87

    Re: Rounding double value

    Thanks danhicksbyron, i used floor and got the desired result.

Similar Threads

  1. how can avoid this rounding off in TReal
    By sukriti.sym in forum Symbian
    Replies: 1
    Last Post: 2009-05-26, 09:22
  2. Rounding Number.
    By kamlesh.sangani in forum Symbian
    Replies: 10
    Last Post: 2008-01-02, 13:34
  3. Rounding of mediatime value in different devices
    By ziyaaf in forum Mobile Java General
    Replies: 1
    Last Post: 2007-08-22, 11:50
  4. Rounding of mediatime in different devices
    By ziyaaf in forum Mobile Java Media (Graphics & Sounds)
    Replies: 1
    Last Post: 2007-08-17, 11:27
  5. how can i calculete pow(double a, double b) logic in J2ME
    By mottaisami in forum Mobile Java General
    Replies: 3
    Last Post: 2005-09-10, 12:31

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×