format query

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

format query

Post by cormullion »

I'm don't fully understand the 'f' and 'g' options in the format function. What are they doing here:

Code: Select all

> (format {%g} 11234.17)
"11234.2"
> (format {%f} 11234.17)
"11234.170000"
> 
How does one get format to print exactly what it's given (in this case), without trailing zeros?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The "%g" format chooses either the "%f" or the "%e" format depending on the size/precision of the number:

Code: Select all

> (format {%g} 134.178)
"134.178"
> (format {%g} 134.1789)
"134.179"
> (format {%g} 12345.6)
"12345.6"
> (format {%g} 12345.67)
"12345.7"
> (format {%g} 123456.7)
"123457"
> (format {%g} 1234567)
"1.23457e+06"
> 
The number gets rounded to 6 digits precicion, and if there are more than 6 before the decimal point then"%g" switches to the "%e" format.

You can force this rounding effect to certain precision by specifiying field width and precision in the "%w.pg" format:

Code: Select all

> (format {%8.4g} 12.34)
"   12.34"
> (format {%8.4g} 12.345)
"   12.35"
> (format {%8.4g} 12345)
"1.234e+04"
> (format {%8.4g} 1234)
"    1234"
> 
The format coding used in newLISP comes from the C-language and is used by most programming languages today, i.e. Perl, Python and Ruby.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks. I'll avoid %g...;-) But how do I get a number printed out without rounding but without added zeros:

Code: Select all

(format {%f} 11234.17)
;-> "11234.170000"
ie I want as many digits as there are, but no more... I suppose I could trim the zeroes off...

Code: Select all

> (trim (format {%f} 11234.17) "0")
"11234.17"
but then:

Code: Select all

> (trim (format {%f} 11234) "0")
"11234."

Sammo
Posts: 180
Joined: Sat Dec 06, 2003 6:11 pm
Location: Loveland, Colorado USA

Post by Sammo »

How about another approach?

Code: Select all

> (string 11234.170000)
"11234.17"
> (string 11234.0)
"11234"
> (string 11234)
"11234"

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Good idea - it certainly works in many cases! My education is still in progress about number precision... ;-) but Lutz says (http://newlisp-on-noodles.org/wiki/inde ... :_Fractran)
string will cut off all digits after the 10th decimal digit
so it should be used carefully...

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Using 'string' is like using the "%1.10g" format and will limit the number to 10 digits precision.

- The 'g' format will automatically cut of trailing zero's.

- the 10 tells it to do maximum 10 digits precicion overall before and after the decimal point.

- the 1 for the width field forces it to use the least amount of space possible. If you would specify "%20.10g" the printout would be in a 20 spaces wide field.

To sum it up: Use the %w.pg" format it gives you the most control and cuts of trailing 0's

Code: Select all

> (format "%1.10g" 12345.670000)
"12345.67"
> (format "%1.10g" 1234567.670000)
"1234567.67"
> (format "%1.10g" 123456789.670000)
"123456789.7"
> (format "%20.10g" 123456789.670000)
"         123456789.7"
> 
The last example shows the 'w' field with 20 right-aligning the number into it. I also shows the 10th digit rounded, because the maximum precision specified is reached by the number.

Lutz

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

.. and I forgot the maximum precision for the p in "%w.pg" would be 15.

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

This is good, thanks. One day it might go into the manual, perhaps...!

cheers Lutz

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

The current description of 'format' in the newLISP manual is terse. There is a good summary of the functioning of all formatting characters in:

http://en.wikipedia.org/wiki/Printf

On the page scroll down to the heading: "printf format placeholders"

Note that some features are omitted in newLISP. Similar to regular expression syntax, C-language like formatting is today one of the few features emerging as standards in all programming languages.

Lutz

Locked