• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

display

System.out.printf("Sum of %.4f\n" +
              "and    %.4f\n" +
              "is     %.4f",number1,number2, sum);
      
Sum of 11.1234
and      12.2345
  is        23.3579

Sum of 11.3456
and    1234.9870
is     1246.3326

how to make display it in this format  11.3456
                                                 123.9870
                                               1246.3326

PS: I really want to make the . on the same line no matter number of significant digit. Thanks.
0
valleytech
Asked:
valleytech
  • 8
  • 7
1 Solution
 
CEHJCommented:
Do
System.out.printf("Sum of %.4f and %.4f is %.4f\n",number1,number2, sum);

Open in new window

0
 
valleytechAuthor Commented:
oh i want to display in a format that u learned during elementary school.

 such as     10.12
               +101.123
        =      111.244
 so the "." are on a line so user can manually check it :)
0
 
CEHJCommented:
OK
System.out.printf("%.4f + %.4f = %.4f\n",number1,number2, sum);

Open in new window

0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
CPColinSenior Java ArchitectCommented:
CEHJ,

He wants to display the numbers in a column, aligned by their decimal points.
0
 
valleytechAuthor Commented:
i know how to display same number of significant digit. However, I want
                   10.12
               +101.123
        =       111.244

or
          111.222
     +   222.333
  =      333.555

0
 
valleytechAuthor Commented:
thanks CPColin.
 You got my question .
0
 
CEHJCommented:
Sorry - misunderstanding. Try
        System.out.printf("%-7s %.4f\n%-8s%.4f\n%-8s%.4f\n",
        "Sum of",
        number1,
        "and",
        number2,
        "is",
        number1 + number2);

Open in new window

0
 
valleytechAuthor Commented:
Enter first  number:  11.45
Enter second number:  22.36
Sum of  11.4500
and       22.3600
is          33.8100

however,
Enter first  number:  112.32
Enter second number:  12.345
Sum of  112.3200
and       12.3450
is          124.6650

The problem will show if you don't have same numbers of digit in front of or after  decimal point
0
 
CEHJCommented:
Your can try this then
        System.out.printf("%-7s % 15.4f\n%-8s% 15.4f\n%-8s% 15.4f\n",
        "Sum of",
        number1,
        "and",
        number2,
        "is",
        number1 + number2);

Open in new window

0
 
valleytechAuthor Commented:
let me try. thanks.
0
 
valleytechAuthor Commented:
cool. It works the way i want. Could you please explain about it? THanks.
0
 
CEHJCommented:
It's to do with flags and width in the Formatter. See docs at

http://java.sun.com/javase/6/docs/api/java/util/Formatter.html
0
 
valleytechAuthor Commented:
i think Format.printf() can handle the job too :)
0
 
CEHJCommented:
Yes it's the same thing essentially
0
 
valleytechAuthor Commented:
sorry i think Format.printf() can handle the job too :) <<--- never mind
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now