Java sprintf equivalent?

Discussions

Performance and scalability: Java sprintf equivalent?

  1. Java sprintf equivalent? (8 messages)

    In C, the sprintf function will format a string, including integer values to a specific length. for example, you could do sprintf(%i03); and it would show 000 as part of the string. Is there a Java equivalent to this function?

    Threaded Messages (8)

  2. Java sprintf equivalent?[ Go to top ]

    No.
  3. Java sprintf equivalent?[ Go to top ]

    Hi,

    I am not sure, but it looks like there is no "sprintf-kind" functions (methods) in Java standard libraries. You can try to use some of the format classes from java.text package, for example: there is class java.text.DecimalFormat which can format decimal values to string based on the given format (mask).

    Best regards,

    Taras
  4. In order to solve that we can use the following:


    NumberFormat nf = NumberFormat.getInstance();
    // say we need 3 digits
    nf.setMinimumIntegerDigits(3);
    System.out.println("Number 1:"+ nf.format(1));
  5. Java sprintf equivalent?[ Go to top ]

    You might want to check this out:

    http://sharkysoft.com/software/lava/jprintf/

  6. Java sprintf equivalent?[ Go to top ]

    There is a Java Class that implements this functionality that is made freely available on www.javasoft.com under Java Developers Connection. I gather from the articles that efforts are under way to support such functionality as part of J2SE/J2EE platform in future but in the mean time this class might meet your needs. It is available with full source code,examples and doc.The name of the class is PrintfFormat and the url is http://developer.java.sun.com/developer/technicalArticles/Programming/sprintf/

    Hope this helps.
    Praveen

  7. Java sprintf equivalent?[ Go to top ]

    Thanks for all of the responses. I did find a Class called PrintfFormat on the javasoft site. It works well, you may want to check it out.
  8. Re: Java sprintf equivalent?[ Go to top ]

    In C, the sprintf function will format a string, including integer values to a specific length. for example, you could do sprintf(%i03); and it would show 000 as part of the string. Is there a Java equivalent to this function?
    hi there, in java 5, there is a static method in the java.lang.String class called format(). it pretty much works the same as C's sprintf(). http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html BR, ~A
  9. Java sprintf equivalent?[ Go to top ]

    You've probably already figured this out, but you can use String.format() about like C's sprintf...

    String mstr = String.format("%-25s %3s/%3s", sa[44].trim(),sa[42].trim(),sa[3].trim());

    Hope this helps