J2ME - How to convert double to string without the power to 10 representation (E-05)
Asked Answered
D

4

6

I have a double value . I want to store it in String with out E notation (in J2ME)

Example

Double value 6.887578324E9 Want to show as 6887578342

Digestion answered 13/3, 2011 at 5:34 Comment(0)
T
6

You can create your own method to do that, or use some already existing library. Javolution, for example, gives you the class and method TypeFormat.format(double d, int digits, boolean scientific, boolean showZero, Appendable a) Check Javolution, it has lots of nice utilities, but if the only thing you need is to format numbers, just write your own method. Here is a quick hack for big numbers

    private static String nosci(double d) {
    if(d < 0){
        return "-" + nosci(-d);
    }
    String javaString = String.valueOf(d);
    int indexOfE =javaString.indexOf("E"); 
    if(indexOfE == -1){
        return javaString;
    }
    StringBuffer sb = new StringBuffer();
    if(d > 1){//big number
        int exp = Integer.parseInt(javaString.substring(indexOfE + 1));
        String sciDecimal = javaString.substring(2, indexOfE);
        int sciDecimalLength = sciDecimal.length();
        if(exp == sciDecimalLength){
            sb.append(javaString.charAt(0));
            sb.append(sciDecimal);              
        }else if(exp > sciDecimalLength){
            sb.append(javaString.charAt(0));
            sb.append(sciDecimal);
            for(int i = 0; i < exp - sciDecimalLength; i++){
                sb.append('0');
            }
        }else if(exp < sciDecimalLength){
            sb.append(javaString.charAt(0));
            sb.append(sciDecimal.substring(0, exp));
            sb.append('.');
            for(int i = exp; i < sciDecimalLength ; i++){
                sb.append(sciDecimal.charAt(i));
            }
        }
      return sb.toString();
    }else{
        //for little numbers use the default or you will
        //loose accuracy
        return javaString;
    }       


}
Tail answered 13/3, 2011 at 23:56 Comment(0)
M
3

Have a look at this article: Converting Double to String without E notation

You can make use of the [NumberFormat][2] or [DecimalFormat][3] class to acheive what you are looking for.

Here is the code:

NumberFormat f = NumberFormat.getInstance();
f.setGroupingUsed(false);
String refinedNumber = f.format(doubleVariable);
Massimiliano answered 13/3, 2011 at 5:43 Comment(5)
Thanks for the reply. I think NumberFormat is available only in JAVA. Not in J2ME.Digestion
well, they seems to be available: download.oracle.com/javame/config/cdc/ref-impl/fp1.1.2/jsr219/…Massimiliano
NumberFormat is java.text . When I try to import java.text I can see only java.io.*, java.lang.*, java.lang.ref.*, java.nio.*, java.rmi.*, java.util.* under “java. “. Am I missing something? I am using blackberry Java plug-in for Eclipse. Let me try ...Digestion
I don't think its going to work :(. have a look at this: java-forums.org/netbeans/… (At the end, they take about blackberry as well)Massimiliano
-1 NumberFormat is not available in Java ME CLDC versions 1.0 (JSR 37) and 1.1 (JSR 139)Toilsome
D
3

You can use Java Formatter, just specify how many decimals do you want with the string.

E.g. a double converted to a number with one decimal:

Formatter format = new Formatter();
String mystring = format.formatNumber(DOUBLENUMBERVARIABLE,1);
System.out.println(mystring );
Declaim answered 22/10, 2012 at 16:19 Comment(1)
J2ME doesnt have a Formatter class.Hebbel
R
0

Normally you can use piece of code below, but not for j2me.

BigDecimal.valueOf(doubleVariable)
Rhamnaceous answered 29/3, 2012 at 8:54 Comment(2)
-1 BigDecimal is not available in Java ME CLDC versions 1.0 (JSR 37) and 1.1 (JSR 139)Toilsome
ooh you are right I just missed the J2me keyword. the sentence I wrote above is very assertive. thank you for your comment.Rhamnaceous

© 2022 - 2024 — McMap. All rights reserved.