help with number formats

Hi everybody, I have a function that detects the . of a number like ( 3.240808 ) and gives me 3.24 only. It works fine until now, when I received as a parameter : -1.55E-6 So I decided to, after I receive my parameter change it from exponential representation to regular, anybody knows how to do that ? Can I at least detect in which format my number is coming ? Thx

Is your parameter coming in as a String? If so, you can convert it to a 'Double', then get the wrapped 'double' value, and use "java.text.DecimalFormat" to display only two digits.

The output is: DBL: 12345.5643 DBL: 12345.56 DBL: -1.55E-6 DBL: -0.00

Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168

posted Nov 11, 2003 11:02:00

0

Thx I�ll try that. Im still wondering if there is a function that could force a number not to use a scientific format...

Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399

posted Nov 12, 2003 10:14:00

0

Keep in mind that there aren't multiple ways of storing real numbers (other than float and double): all of them conform to the IEEE 754 standard. There is no notion of some real numbers being stored in scientific notation and some in another format: all real numbers are stored the same way in Java. The difference is when the number gets printed out. And using "DecimalFormat" you have complete control over that. Compile and run this code as an example:

When I run on Win2K using JDK1.4.2 the results are: D1: 5.4E-6 D2: 5.4E-6 So both "d1" and "d2" refer to the same value, there are just different ways of representing it in "human-readable" form. You don't like scientific notation? Then get in the habit of using "DecimalFormat" to control the display. I'm unaware of a way of registering a defaul decimal format when you start the JDK, but if there is you could control it at that level. [ November 12, 2003: Message edited by: Wayne L Johnson ]

Giselle Dazzi
Ranch Hand

Joined: Apr 20, 2003
Posts: 168

posted Nov 13, 2003 10:29:00

0

Thanks I used it and it�s working now. It�s not that I dont like scientific notation, the problem was that this method was using the location of the period to do something, and with scientific notation, the period is not in the "real" place...