We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

# display

on
Medium Priority
202 Views
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.
Comment
Watch Question

## View Solution Only

CERTIFIED EXPERT
Top Expert 2016

Commented:
Do
``````System.out.printf("Sum of %.4f and %.4f is %.4f\n",number1,number2, sum);
``````

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 :)
CERTIFIED EXPERT
Top Expert 2016

Commented:
OK
``````System.out.printf("%.4f + %.4f = %.4f\n",number1,number2, sum);
``````
Senior Java Architect

Commented:
CEHJ,

He wants to display the numbers in a column, aligned by their decimal points.

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

Commented:
thanks CPColin.
You got my question .
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry - misunderstanding. Try
``````        System.out.printf("%-7s %.4f\n%-8s%.4f\n%-8s%.4f\n",
"Sum of",
number1,
"and",
number2,
"is",
number1 + number2);
``````

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
CERTIFIED EXPERT
Top Expert 2016
Commented:
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);
``````

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
let me try. thanks.

Commented:
cool. It works the way i want. Could you please explain about it? THanks.
CERTIFIED EXPERT
Top Expert 2016

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

Commented:
i think Format.printf() can handle the job too :)
CERTIFIED EXPERT
Top Expert 2016

Commented:
Yes it's the same thing essentially

Commented:
sorry i think Format.printf() can handle the job too :) <<--- never mind
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
##### Thanks for using Experts Exchange.

Limited access with a free account allows you to:

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile