For Programmers: Free Programming Magazines  


Home > Archive > Java Help > July 2006 > Question about decimal alignment









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Question about decimal alignment
Mike

2006-07-25, 10:01 pm

I am writing a program for my class and part of the out is a series of
numbers with 2 decimal places. How do I get the decimal places to line
up?

I get alignment that looks like:

775.00
30.25
1.15

When what I want is:

775.00
30.25
1.15

alined.

The snippit of code is:

System.out.printf( "\n DVD Results\n");
System.out.printf( "DVD Title Qty Cost
Value\n");
System.out.printf(
"-------------------------------------------------------" );

for (int i = 0; i < index1; i++)
{
// Display DVD details

System.out.printf( "\n%-5s",id[i] );
System.out.printf( " %-25s",name[i] );
System.out.printf( " %3.0f",qty[i]);
System.out.printf( " %-4.2f",each[i]);
System.out.printf( " %6.2f",total[i]);
}

System.out.printf(
"\n-------------------------------------------------------\n" );
System.out.printf( "
%6.2f",gtotal1 );
System.out.printf( "\n
==================\n" );

Bart Cremers

2006-07-26, 4:03 am


Mike schreef:

> I am writing a program for my class and part of the out is a series of
> numbers with 2 decimal places. How do I get the decimal places to line
> up?
>
> I get alignment that looks like:
>
> 775.00
> 30.25
> 1.15
>
> When what I want is:
>
> 775.00
> 30.25
> 1.15
>
> alined.
>


For formatting using the %f notation, remember that the format is
<width>.<precision>, where width is the total width of the field and
precision is the number of decimal places.

So for outputting 775.00 you need at least a width of 6.

Regards,

Bart

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com