Re: Printing numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg54552] Re: Printing numbers
- From: Bill Rowe <readnewsciv at earthlink.net>
- Date: Tue, 22 Feb 2005 04:25:50 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
On 2/21/05 at 3:44 AM, stevebg at adelphia.net (Steve Gray) wrote: >1. Can anyone tell me why these results are what they are? Neither >answer is what I would expect, which is 10. The two strings are the >same length. >Print[StringLength[ToString[123.456789]], " ", >StringLength[ToString[1234567.89]]]; >7 22 Look at the strings returned in each case. What you are seeing is the result of the default displays Mathematica uses. In the first example, Mathematica by default displays only three decimals to the right of the decimal point resulting in a seven character long string. In the second example, by default Mathematica displays the number in scientific form with 6 significant digits. Since the string form of this must include the exponent, the result is a longer string. >2. Is there a simple, easily available or self-evident way to get >numbers printed out such that the total number of spaces occupied >by the print before the decimal point is some fixed, user-specified >amount, the number of spaces occupied after the decimal point is a >different user-specified amount, and the number of spaces following >the number is a third user-specified amount. Yes. Take a look at the documentation for PaddedForm and NumberForm. For example, In[19]:= (PaddedForm[#1, {8, 3}] & ) /@ {345.678, 34.5678, -345.678} Out[19]= {PaddedForm[345.678, {8, 3}], PaddedForm[34.5678, {8, 3}], PaddedForm[-345.678, {8, 3}]} In[20]:= (StringLength[ToString[#1]] & ) /@ % Out[20]= {10, 10, 10} -- To reply via email subtract one hundred and four