×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Exclamation URGENT: J2ME double format

    i have a big problem: the phone displays 0.0005 as 5.0E4. i want it to display 0.0005, not 5.0E4.
    How can i do that sine java.text with NumberFormat and DoubleFormat methods are not implemented in j2me?

    Many thanks!

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: URGENT: J2ME double format

    Essentially, by writing your own number formating code.

    Or try...

    PHP Code:
        private static String formatDouble(double d) {
            
    String s Double.toString(d);
            
            if (!(
    Double.isNaN(d) || Double.isInfinite(d))) {
                
    char[] ch s.toCharArray();
                
    boolean isNegative ch[0] == '-';

                
    StringBuffer digits = new StringBuffer(ch.length);
                
    StringBuffer exp = new StringBuffer(ch.length);
                
    boolean foundE false;
                
    int point 0;
                for (
    int i 0ch.lengthi++) {
                    
    char c ch[i];
                    if (
    == 'E') {
                        
    foundE true;
                    } else if (
    >= '0' && <= '9') {
                        if (
    foundE) {
                            
    exp.append(c);
                        } else {
                            
    digits.append(c);
                        }
                    } else if (
    == '-' && foundE) {
                        
    exp.append(c);
                    } else if (
    == '.') {
                        
    point digits.length();
                    }
                }
                if (
    exp.length() > 0) {
                    
    point += Integer.parseInt(exp.toString());
                }
                while (
    point 1) {
                    
    digits.insert(0'0');
                    
    point++;
                }
                while (
    point >= digits.length()) {
                    
    digits.append('0');
                }
                
    digits.insert(point'.');
                if (
    isNegative) {
                    
    digits.insert(0'-');
                }
                
    boolean done false;
                do {
                    
    int last digits.length() - 1;
                    
    char c digits.charAt(last);
                    if (
    == '0' || == '.') {
                        
    digits.deleteCharAt(last);
                    }
                    if (
    != '0') {
                        
    done true;
                    }
                } while (!
    done);
                
                
    digits.toString();
            }
            return 
    s;
        } 
    I haven't tested this very well, so use at your own risk.

    Graham.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Re: URGENT: J2ME double format

    i don't get it... how to use..i mean i need a function like doubleFormat(double d, int n) where d is the number and n the number o decimals i want to display

  4. #4
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: URGENT: J2ME double format

    You pass it a double, you get a String. I'm afraid if you want something more sophisticated, you'll have to write your own.

    Graham.

Similar Threads

  1. URGENT J2ME Bluetooth Conection problem
    By lparmy in forum Mobile Java Networking & Messaging & Security
    Replies: 2
    Last Post: 2010-01-09, 19:35
  2. J2ME or C++ for VoIP on Mobile
    By sandeepkumar03 in forum VoIP
    Replies: 4
    Last Post: 2009-02-02, 10:28
  3. need urgent help:is awt package support in j2me?
    By avanidave in forum Mobile Java General
    Replies: 2
    Last Post: 2007-03-08, 13:28
  4. J2ME Game Developer Required - Urgent
    By msureka1 in forum News and Announcements
    Replies: 1
    Last Post: 2006-10-30, 07:46
  5. urgent problem: file format unknow for xhtml
    By mingl in forum Browsing and Mark-ups
    Replies: 1
    Last Post: 2006-02-15, 08:16

Posting Permissions

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