How to change 3 to "003"

F

fishfry

How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.
 
I

IchBin

fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.
Try looking at 'java.text.DecimalFormat'

--


Thanks in Advance...
IchBin
__________________________________________________________________________

'The meeting of two personalities is like the contact of two chemical
substances:
if there is any reaction, both are transformed.'
- Carl Gustav Jung, (1875-1961), psychiatrist and psychologist
 
P

pet0etie

fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.

hello,

this should work ... but you can always search google on examples of the use
of "DecimalFormat"
greetz,
pet0etie

/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
/\/\/\/\/\/\/\

public class someClass {
// number of digits to show (3 in tour example but easy to modify)
private int numDigits = 3;

public someClass (int value) {
System.out.print("Your original value " + value + " becomes : " +
formatValue(value));
}

private String formatValue() {
private DecimalFormat myFormat = createFormat(numDigits);
return myFormat.format(Value);
}

private DecimalFormat createFormat(int numDigits){
// Create a String that is all 0's, and the length is numDigits.
StringBuffer zeros = new StringBuffer();
for (int digit = 1; digit <= numDigits; digit++) {
zeros.append("0");
}
// Now use that to create a DecimalFormat object.
return new DecimalFormat(zeros);
}
}
 
C

Chris Smith

pet0etie said:
private DecimalFormat createFormat(int numDigits){
// Create a String that is all 0's, and the length is numDigits.
StringBuffer zeros = new StringBuffer();
for (int digit = 1; digit <= numDigits; digit++) {
zeros.append("0");
}
// Now use that to create a DecimalFormat object.
return new DecimalFormat(zeros);
}

It may be easier to do it this way:

NumberFormat format = NumberFormat.getInstance();
format.setMinimumIntegerDigits(numDigits);

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
S

Starshine Moonbeam

Chris Smith said:
It may be easier to do it this way:

NumberFormat format = NumberFormat.getInstance();
format.setMinimumIntegerDigits(numDigits);

DecimalFormat's pretty easy breezy...

int i = 3;
DecimalFormat df = new DecimalFormat("#000");
df.format(i);
 
T

Tony Morris

fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.

public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}
 
S

Starshine Moonbeam

Tony said:
fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.

public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}

What's printf? I only know print and println.
 
T

Tony Morris

Starshine Moonbeam said:
Tony said:
fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.

public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}

What's printf? I only know print and println.

It's a method.

http://java.sun.com/j2se/1.5.0/docs/api/java/io/PrintStream.html#printf(java.lang.String,
java.lang.Object...)
 
C

Chris Smith

Tony Morris said:
It's a method.

Note that it's also rather new. You'll need the 1.5.0 (aka 5.0) version
of the Java platform to use it. That's what you probably ought to be
using for new development anyway, but it will take several existing
systems a while to catch up, so you'll have to wait on those.

Also note that (perhaps to make a point) Tony's code worked as you want
for your specific test input of 3, but probably wouldn't do what you
want for inputs outside the range of [0..9]. The point Tony might have
been trying to make is that you never really specified what you want to
see happen in many cases. That also lies behind the choice between
DecimalFormat or NumberFormat in other forks of this same thread.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
W

Walter Mitty

Tony said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.


public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}

Is this a joke reply? This solution is no different to saying the answer
to the question is

String s="003";
 
T

Tony Morris

Walter Mitty said:
Tony said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.


public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}

Is this a joke reply? This solution is no different to saying the answer
to the question is

String s="003";

It meets the stated requirement.
The response was no more of a joke than the request for assistance.
I cannot force someone to state their real requirement.
The stated solution differs somewhat to your provided analogy, depending on
context, which clearly is needed to provide a more complete answer.
Speculation is a best case scenario as it stands.
 
T

The Abrasive Sponge

Tony said:
Tony said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.


public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}

Is this a joke reply? This solution is no different to saying the answer
to the question is

String s="003";


It meets the stated requirement.
The response was no more of a joke than the request for assistance.
I cannot force someone to state their real requirement.
The stated solution differs somewhat to your provided analogy, depending on
context, which clearly is needed to provide a more complete answer.
Speculation is a best case scenario as it stands.

Damn, that was nice prose for a java newsgroup.
 
L

Lee Fesperman

Tony said:
fishfry said:
How do I convert an int to a 3 (or n) -digit string? I found a couple of
third-party implementations of sprintf(), but I was wondering what's the
officially correct way to do this.

public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}

I didn't check the docs, but if Java 5 printf() is the same as C, then you can use:

System.out.printf("%03d", x);
 
W

Walter Mitty

The said:
Tony said:
Tony Morris wrote:



How do I convert an int to a 3 (or n) -digit string? I found a
couple of
third-party implementations of sprintf(), but I was wondering
what's the
officially correct way to do this.



public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}


Is this a joke reply? This solution is no different to saying the answer
to the question is

String s="003";



It meets the stated requirement.
The response was no more of a joke than the request for assistance.
I cannot force someone to state their real requirement.
The stated solution differs somewhat to your provided analogy,
depending on
context, which clearly is needed to provide a more complete answer.
Speculation is a best case scenario as it stands.

Damn, that was nice prose for a java newsgroup.

It was arrogant geek coverup code. Nothing more.

--
Walter Mitty
-
Useless, waste of money research of the day :
http://news.bbc.co.uk/2/hi/science/nature/4083517.stm
http://www.tinyurl.com
 
J

Jon Caldwell

Walter said:
The said:
Tony said:
Tony Morris wrote:



How do I convert an int to a 3 (or n) -digit string? I found a
couple of
third-party implementations of sprintf(), but I was wondering
what's the
officially correct way to do this.




public class X
{
public static void main(String[] args)
{
int x = 3;
System.out.printf("00%d", x);
}
}


Is this a joke reply? This solution is no different to saying the
answer
to the question is

String s="003";




It meets the stated requirement.
The response was no more of a joke than the request for assistance.
I cannot force someone to state their real requirement.
The stated solution differs somewhat to your provided analogy,
depending on
context, which clearly is needed to provide a more complete answer.
Speculation is a best case scenario as it stands.

Damn, that was nice prose for a java newsgroup.


It was arrogant geek coverup code. Nothing more.
Use the NumberFormat class. Set the minimum and maximum integer digits to 3.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top