• 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

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
check if NumberFormat class of Java solves your problem.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
'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().
 
Stephen Tron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Get me the mayor's office! I need to tell him about this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic