[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 189
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CPColinCommented:
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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