# Dealing with floating-point in double and String and convert it to String

• 02-10-2012, 03:54 PM
Dealing with floating-point in double and String and convert it to String
My question is a little bit simple but makes me crazy! :P:

Suppose that:

Code:

`long number_long = 1100903354159269000;`
I want to show this number as String in output like "1100903354.159269000". To do this, I convert the number to String and perform some basic concatination and it is ok. But I need this new number as Long or Float exactly like this floating point to do some mathematical functions.

How can I do this? The place of floating point is fixed, but the left part of it may increase.
Could you tell me which one is better, Float or Double? How can I convert the result to String with this floating point?
• 02-10-2012, 04:13 PM
Tolls
Re: Dealing with floating-point in double and String and convert it to String
If you need accuracy use BigDecimal.
Not sure what you mean by the point being fixed.
• 02-10-2012, 07:13 PM
mkarthik90
Re: Dealing with floating-point in double and String and convert it to String
I you need to convert the string to a Float better use the float class constructor as below

String str="111.00001";
Float f = new Float(str);

The string will be converted to a float and you can use for furthur processing.
• 02-10-2012, 07:26 PM
Re: Dealing with floating-point in double and String and convert it to String
Quote:

Originally Posted by Tolls
If you need accuracy use BigDecimal.
Not sure what you mean by the point being fixed.

WOW! I have not ever seen BigDecimal class! Wonderful and perfect!!
I've done it, it's working, yessssss :(y):

Quote:

Originally Posted by mkarthik90
I you need to convert the string to a Float better use the float class constructor as below

String str="111.00001";
Float f = new Float(str);

The string will be converted to a float and you can use for furthur processing.

Thanks for your reply, but I think the number will become greater than float type variable lenght. BigDecimal meet my needs.