my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Number to String ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Number to String ?" Watch "Number to String ?" New topic
Author

Number to String ?

sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Hi All,

My question is related with converting a NUMBER TO STRING..

Say, i have a variable of type Double as shown:

double t=42.5;

now , i make use of a static method "toString" of class Double to convert this number to a string...

String mydouble=Double.toString(t);
System.out.println(mydouble); //this statement outputs the value as : 42.5

My question is since the output is also a number, what is the meaning of 'converting a number to string' since i do have a impression of string representation...

Where as...System.out.println(t); this also outputs the same value i.e 42.5.


At the end of the day, my question is when both println statments gives the same value i.e 42.5, what is the actual role played by the method toString() method.

Thank You





:roll: :roll:
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

Every class implements toString() because it is defined by Object.

toString() allows you to get something, meaningful representation of a given object.

All of the wrapper classes provide an overloaded toString() (static) method that takes a primitive value of the correct type and returns a String.



Regards, Sud.
SCJP 5 ScjpFAQ JLS
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Your number comes out in 1s and 0s which the processor can work out mean 42.5. It is stored in a format which represents a number. There is a method something like "toBinaryString" in the Double class which lets you see the 1s and 0s, but it omits leading 0s; remember a double is 64 bits.

Your Double.toString(d) comes out as a "4" then a "2" then a "." then a "5". It is stored in a format which represents writing. There are methods which allow you to see those 4 characters, eg System.out.println(). They don't omit leading 0s and a String is however long it happens to be.

Remember the age-old question: How long is a String?
sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Originally posted by Campbell Ritchie:
It is stored in a format which represents writing. There are methods which allow you to see those 4 characters, eg System.out.println(). They don't omit leading 0s and a String is however long it happens to be.

Remember the age-old question: How long is a String?[/QB]
sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Originally posted by sameer khazi:
[QB][/QB]


Hi Mr Cambell..

Well you have made the concept clear as how the numbers are actually represented when output.

Can you please elaborate a bit on the following..." represents a writing".
This may clear my remaining doubt.

Thank you
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
Anything which can be put in writing can be put into a String. This posting contains a String. You can't change to bold or italics in the middle of a String, but anything you can write on paper can be put into a String.
sameer khazi
Greenhorn

Joined: Sep 05, 2008
Posts: 28
Thank you Mr Shekhar and Mr Campbell for your inputs.

When i find no one nearby to clear my doubts i simply log-in to JavaRanch.
It seems i have found a good tutor in "it".
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37936
    
  22
You're welcome and it is good to see we are doing a good job. Thank you.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Number to String ?
 
Similar Threads
HELP! Need to disable scientific notation
Need Helping Sorting Array of Doubles But Need To Know Index Position
maintaining a set of arrays of doubles
Decimal places
binary of a float