1

Most sites give me same answer, which is Math.round() but J2ME does not have same function. I want a more specific one. I'm really having trouble on this.

Example, I have this value:

double x = 5.449681394262832E-6;

I want it to become: 5.45; with no exponents and only two decimal places..Please don't give me answers for Java, I need more specific for J2ME.

David
  • 15,894
  • 22
  • 55
  • 66
  • Have a look at [this question](http://stackoverflow.com/questions/4930598/is-there-a-round-off-function-in-j2me) and also at [this one](http://stackoverflow.com/questions/1451149/rounding-a-double-to-5-decimal-places-in-java-me). Removing the exponent looks like a bad idea to me, unless you are always dealing with the same power of ten. If so, then your best option is probably to remove the exponent using string manipulation, then parse the remaining string to double and round as described in the above mentioned questions. – Mister Smith Sep 24 '13 at 08:55
  • You could also use [MicroFloat](http://www.dclausen.net/projects/microfloat/). – Mister Smith Sep 24 '13 at 08:56

1 Answers1

0

You can create an String from that double, find the '.' and the substring from that.

private static void noExponentValue(double x) {
    String s = String.valueOf(x);
    int i = s.indexOf('.');
    if (i < 0) {
        System.out.println(s + ".00");
    } else {
        i = Math.min(i + 3, s.length());
        System.out.println(s.substring(0, i));
    }
}
Telmo Pimentel Mota
  • 4,033
  • 16
  • 22
  • please do you think you could help me with this question https://stackoverflow.com/questions/23730176/j2me-how-to-parse-a-byte-array-into-xml-and-then-read-and-display-specific-data – Axel May 19 '14 at 12:22