• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

help with number formats

 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic