double to string

Z

zamba

hi, i want to know how to convert a doble value like this 98.34 to
string 00009834 or 9.34 to 00000934 thanks a lot !!!!!
 
F

Florian Huebner

zamba said:
thank you , but i read link and i tried to use NumberFormat , but i
can't get an example how to use exactly the way i need...

In that case I would just replace the "." with and empty string, count
the remaining letters and add as many zeros as you need.
 
T

Tim Slattery

zamba said:
hi, i want to know how to convert a doble value like this 98.34 to
string 00009834 or 9.34 to 00000934 thanks a lot !!!!!

Check out the DecimalFormat class.
 
A

Andreas Leitgeb

Tim Slattery said:
Check out the DecimalFormat class.

I guess, it would be easier, if he(zamba) first multiplied with
100, and then used some integral format.
 
H

Hal Rosser

zamba said:
hi, i want to know how to convert a doble value like this 98.34 to
string 00009834 or 9.34 to 00000934 thanks a lot !!!!!

I think it goes something like this:

assume you have a double n with a value of 9.34 which you want to display as
009.34 string.
NumberFormat nf = NumberFormat.getNumberInstance();
//set the min digits before the decimal
nf.setMinimumIntegerDigits(3);
//set min digits after decimal
nf.setMinimumFractionalDigits(2);
String numberToShow = nf.format(n);
//numberToShow will be your string
 
H

Hal Rosser

Hal Rosser said:
I think it goes something like this:

assume you have a double n with a value of 9.34 which you want to display
as 009.34 string.
NumberFormat nf = NumberFormat.getNumberInstance();
//set the min digits before the decimal
nf.setMinimumIntegerDigits(3);
//set min digits after decimal
nf.setMinimumFractionalDigits(2);
String numberToShow = nf.format(n);
//numberToShow will be your string
But to follow up: You would not format 98.34 to display as "00009834"
because then it would not be the same number.
 
J

John W. Kennedy

zamba said:
thank you , but i read link and i tried to use NumberFormat , but i
can't get an example how to use exactly the way i need...

final DecimalFormat df = new DecimalFormat("00000000");
df.setMultiplier(100);
 
J

John W. Kennedy

Andreas said:
I guess, it would be easier, if he(zamba) first multiplied with
100, and then used some integral format.

DecimalFormat.setMultiplier()
 
L

Lew

Hal said:
But to follow up: You would not format 98.34 to display as "00009834"
because then it would not be the same number.

How you format the number for display or storage has no effect on what the
number is.

Plenty of formats use implicit decimal points, so the OP would indeed have
"the same number" given the implicit location of the decimal point.

It is no more sensible to say the number is "not the same" than to say that
the European-format "9,834" does not represent the same number as the
American-format "9.834", or to claim that the European "9.834" does.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top