aspose file tools*
The moose likes Java in General and the fly likes Is it possible to reformat the output from a double value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Is it possible to reformat the output from a double value?" Watch "Is it possible to reformat the output from a double value?" New topic
Author

Is it possible to reformat the output from a double value?

Stephen Tron
Greenhorn

Joined: Sep 20, 2010
Posts: 3
I am working on a method that will return a double with a new format settings.
For example:


public double reformatDouble (double num) {


...code that will refomat the double num to a (9.5) format

return num;

}

So if I call this method, I will receive the following results:

reformatDouble(0.0) --->> this will return a double of value '000000000.00000'
reformatDouble(5.0) --->> this will return a double of value '000000005.00000'
reformatDouble(19.567) --->> this will return a double of value '000000019.56700'

Is it possible to have a double that will output in this format?
I would appreciate any help on this!!

NOTE: I have seen suggestions on other threads on this, but all of them are returning a String e.g. DecimalFormat.format().
However, I need to return a double. Is this possible?
Siddhesh Deodhar
Ranch Hand

Joined: Mar 05, 2009
Posts: 117
check if NumberFormat class of Java solves your problem.


Good, Better, Best, Don't take rest until, Good becomes Better, and Better becomes Best.
Sidd : (SCJP 6 [90%] )
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

'double' and 'float' are internally formatted as IEEE754 always always always and cannot be changed. What you seems to need is a human readable double format as is obtained from DecimalFormat.format().

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Stephen Tron
Greenhorn

Joined: Sep 20, 2010
Posts: 3
Thanks for you comments - much appreciated.

Siddhesh, I've looked at the NumberFormat class but unfortunately, this doesn't solve the problem.

And James, I see what your saying about double & float variables. But how about a 'Double' object instead of a 'double' variable? I've been looking at a way to override some of the methods of the java.lang.Double class but I've had no luck so far.
Do you think this could be a possible solution?
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Stephen Tron wrote:Thanks for you comments - much appreciated.
And James, I see what your saying about double & float variables. But how about a 'Double' object instead of a 'double' variable? I've been looking at a way to override some of the methods of the java.lang.Double class but I've had no luck so far.
Do you think this could be a possible solution?


'Double' is just a wrapper for a 'double' and 'Float' is just a wrapper for a 'float' so both are stored in IEEE format. Both Double and Float are immutable and final so you can change nothing about them.

Despite you saying that NumberFormatter is not what you need I bet that NumberFormat or it's derived class DecimalFormat is what you need and you are actually trying to solve the wrong problem. I suspect you need to stand back a bit from the problem and look at the wider problem. Before you write off DecimalFormat, run this code

Why and when do you need the specific format for the numbers that is different from the output of this code fragment?
Stephen Tron
Greenhorn

Joined: Sep 20, 2010
Posts: 3
The purpose of this is for writing to an existing Database, that may not accept a string as a field.
Hence all this trouble. This has been put on the backburner for now, but when I get all the details
of this problem and have an appropriate solution, I'll post it up.

Thanks for all your help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is it possible to reformat the output from a double value?