Hi there;

I am having problems with using printf method in this simple loop. I am looking for a 3 decimal places in the right one column and a integers in the left. I know how to print this with system.out.println but I dont know how to write it with printf with given conditions.

Can anybody help or give me a link with good and usefull explanation ?

thanks in advance...

public class fourdot3 {

	public static void main(String[] args) {
		double gram = 1;	
		double pound = 0.035;
		for (gram = 1; gram <=199; gram++) {
			System.out.println(gram + "\t"+ (gram * pound));
5 Years
Discussion Span
Last Post by bombay1982
Featured Replies
  • First, use printf instead of println Then check out this link for [URL="http://sharkysoft.com/archive/printf/docs/javadocs/lava/clib/stdio/doc-files/specification.htm"]format specifiers[/URL]: Read More


The answer is

System.out.printf("%1.0f\t%1.03f\n", gram, (gram*pound));

I received that answer from one of the user on my private message. Thank you very much.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.