[program-java] Re: String to int...

  • From: "J. R. Westmoreland" <jr@xxxxxxx>
  • To: <program-java@xxxxxxxxxxxxx>
  • Date: Thu, 24 Sep 2009 13:55:33 -0600

Susie,

Later on in the book he also talks about writing the output to an in memory
string, or an array.
I don't recall just where off hand and would have to look it up. I'll leave
that as an exercise to the reader. <grin> LOL

Bruce has done a pretty good job on the book, hasn't he?

Best,
J. R.

-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Stanzel, Susan -
Kansas City, MO
Sent: Thursday, September 24, 2009 1:43 PM
To: 'program-java@xxxxxxxxxxxxx'
Subject: [program-java] Re: String to int...

Hi Listers,

This might be more than you wanted to know, but we all must work together if
we want to learn this stuff (grin).

Susie Stanzel

2.4.4  Formatted Output 
If you ran the preceding Interest2 example, you might have noticed that the
answer is not always written in the format that is usually used for dollar
amounts. In general, dollar amounts are written with two digits after the
decimal point. But the program's output can be a number like 1050.0 or
43.575. It would be better if these numbers were printed as 1050.00 and
43.58.

Java 5.0 introduced a formatted output capability that makes it much easier
than it used to be to control the format of output numbers. A lot of
formatting options are available. I will cover just a few of the simplest
and most commonly used possibilities here.

You can use the function System.out.printf to produce formatted output. (The
name "printf," which stands for "print formatted," is copied from the C and
C++ programming languages, which have always have a similar formatting
capability). System.out.printf takes two or more parameters. The first
parameter is a String that specifies the format of the output. This
parameter is called the format string. The remaining parameters specify the
values that are to be output. Here is a statement that will print a number
in the proper format for a dollar amount, where amount is a variable of type
double: 

System.out.printf( "%1.2f", amount );
TextIO can also do formatted output. The function TextIO.putf has the same
functionality as System.out.printf. Using TextIO, the above example would
be: TextIO.putf("%1.2",amount); and you could say
TextIO.putf("%1.2f",principal); instead of TextIO.putln(principal); in the
Interest2 program to get the output in the right format. 

The output format of a value is specified by a format specifier. The format
string (in the simple cases that I cover here) contains one format specifier
for each of the values that is to be output. Some typical format specifiers
are %d, %12d, %10s, %1.2f, %15.8e and %1.8g. Every format specifier begins
with a percent sign (%) and ends with a letter, possibly with some extra
formatting information in between. The letter specifies the type of output
that is to be produced. For example, in %d and %12d, the "d" specifies that
an integer is to be written. The "12" in %12d specifies the minimum number
of spaces that should be used for the output. If the integer that is being
output takes up fewer than 12 spaces, extra blank spaces are added in front
of the integer to bring the total up to 12. We say that the output is
"right-justified in a field of length 12." The value is not forced into 12
spaces; if the value has more than 12 digits, all the digits will be
printed, with no extra spaces. The specifier %d means the same as %1d; that
is an integer will be printed using just as many spaces as necessary. (The
"d," by the way, stands for "decimal" (base-10) numbers. You can use an "x"
to output an integer value in hexadecimal form.) 

The letter "s" at the end of a format specifier can be used with any type of
value. It means that the value should be output in its default format, just
as it would be in unformatted output. A number, such as the "10" in %10s can
be added to specify the (minimum) number of characters. The "s" stands for
"string," meaning that the value is converted into a String value in the
usual way.

The format specifiers for values of type double are even more complicated.
An "f", as in %1.2f, is used to output a number in "floating-point" form,
that is with digits after the decimal point. In %1.2f, the "2" specifies the
number of digits to use after the decimal point. The "1" specifies the
(minimum) number of characters to output, which effectively means that just
as many characters as are necessary should be used. Similarly, %12.3f would
specify a floating-point format with 3 digits after the decimal point,
right-justified in a field of length 12.

Very large and very small numbers should be written in exponential format,
such as 6.00221415e23, representing "6.00221415 times 10 raised to the power
23." A format specifier such as %15.8e specifies an output in exponential
form, with the "8" telling how many digits to use after the decimal point.
If you use "g" instead of "e", the output will be in floating-point form for
small values and in exponential form for large values. In %1.8g, the 8 gives
the total number of digits in the answer, including both the digits before
the decimal point and the digits after the decimal point.

In addition to format specifiers, the format string in a printf statement
can include other characters. These extra characters are just copied to the
output. This can be a convenient way to insert values into the middle of an
output string. For example, if x and y are variables of type int, you could
say

System.out.printf("The product of %d and %d is %d", x, y, x*y);
When this statement is executed, the value of x is substituted for the first
%d in the string, the value of y for the second %d, and the value of the
expression x*y for the third, so the output would be something like "The
product of 17 and 42 is 714" (quotation marks not included in output!).  

-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of J. R. Westmoreland
Sent: Thursday, September 24, 2009 2:01 PM
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: String to int...

Jim,

The 1.5 and later versions of java have a facility like the C/C++ sprintf()
function. You can allocate a buffer and just have it write them in there. I
think with a format specifier like "%05d".

Your task is much easier than mine. LOL
You have to love that JNI stuff, right?

Best,
J. R.

-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Thursday, September 24, 2009 12:26 PM
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: String to int...

Adrian:

I'll take a look.

Jim  

-----Original Message-----
From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Adrian Beech
Sent: September 24, 2009 13:46
To: program-java@xxxxxxxxxxxxx
Subject: [program-java] Re: String to int...

G'day Jim,

 

If I understand your question you want the two byte representation of the
integer value when the properties get method is called?  I'd suggest
checking out the java.text classes and perhaps using, if I recall right,
SimpleTextFormat.  I've done something similar in the past to display a
numeric value as a literal which was padded out with leading 0's.

 

Cheers.

AB

 

From: program-java-bounce@xxxxxxxxxxxxx
[mailto:program-java-bounce@xxxxxxxxxxxxx] On Behalf Of Corbett, James
Sent: Thursday, 24 September 2009 11:06 PM
To: program-l@xxxxxxxxxxxxx; program-java@xxxxxxxxxxxxx
Subject: [program-java] String to int...

 

Sorry for the cross posting.... I have a COBOL copybook that defines an
element as Pic 9(5)... 

In my Java class I need to pass into my setter an integer for obvious
reasons. COBOL under the hood needs me to pass 00000 if the field is empty
because I'm actually building the com area at this point. If however the
field contains 999 I must still pass 00999 to fulfil the expected length....

Ok, I have a string "00999" and need to convert it to an integer.... If I do
so, then the leading zeros are stripped off and my com area is 2 bytes
short.... Don't ask why or how it's the way it is....

So, how do I manage this? 

Jim 

James M. Corbett 

Programmer / Analyst
GST/HST Micros | Micros de la TPS/TVH
Business Suite Assessing Systems (BSAS) Revenue and Accounting Systems
Directorate (RASD) | Direction des Systèmes de revenu et de comptabilité
(DSRC) Information Technology Branch (ITB) | Direction générale de
l'informatique (DGI) Canada Revenue Agency | Agence du revenue du Canada 

(613) 941-1338 

"...Is a hippopotamus a hippopotamus, or just a really cool Opotamus?" 



__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4453 (20090924) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com



__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4455 (20090924) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com







Other related posts: