Welcome to the iTechForums.
If this is your first visit, be sure to check out the
FAQ by clicking the link above.
You may have to
register before you can post: click the register link above to proceed.
To start viewing messages, select the forum that you want to visit from the selection below.
To register now click here.
|
Latest Threads
Advertisements
Forum Statistics
Threads:
Posts: 2
Members:
Number of Users Online:
Welcome to our newest member, |
|
 |

05-14-2008, 08:58 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 1
|
|
How can I format a java string using a width variable?
In c, you can do something like this:
int width = 10;
double value = 123456.789012;
buffer = printf("%*f", width, value);
How can i do that in java?
I tried using str = String.format("%*f", width, value);
Java does not recognize what the asteriks is for.
|

05-14-2008, 10:09 PM
|
|
Junior Member
|
|
Join Date: May 2008
Posts: 20
|
|
The Entities in java are slightly different. I adore them being included (finally).
float bigNum = 123456.789012;
String pattern = String.format( "%10.2f is my magic number%n", bigNum);
System.out.println( pattern);
or
System.out.println(
String.format( "%10.3f", bigNum) );
the "decimal place" is the rounding.
lead off with a zero to get leading zeroes
String.format("%03.2f", bigNum));
if I want evenly spaced columns, I have to construct an Object[] array
array[0] = salaryAmt;
array[1] = hourlyRate;
array[2] = "Cowboy Bob";
System.out.println(
"%5.2f,%3.2d,%s%n, array);
|
 |
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
All times are GMT. The time now is 02:18 PM.