format query

Started by cormullion, November 15, 2006, 01:02:54 AM

Previous topic - Next topic

cormullion

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


> (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

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


> (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:



> (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

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


(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...


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


but then:


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

Sammo

#3
How about another approach?
> (string 11234.170000)
"11234.17"
> (string 11234.0)
"11234"
> (string 11234)
"11234"

cormullion

#4
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/index.php/Talk:Quiz_3:_Fractran">//http://newlisp-on-noodles.org/wiki/index.php/Talk:Quiz_3:_Fractran)


Quote
string will cut off all digits after the 10th decimal digit


so it should be used carefully...

Lutz

#5
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


> (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

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



Lutz

cormullion

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



cheers Lutz

Lutz

#8
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">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