comparison lispref/strings.texi @ 53687:4f06a8a0e7a6

For `format', make source and documentation match.
author Jonathan Yavner <jyavner@member.fsf.org>
date Sat, 24 Jan 2004 17:39:47 +0000
parents 6fddc60d9cbc
children 7e9bbc10e031
comparison
equal deleted inserted replaced
53686:ea0b901a1f91 53687:4f06a8a0e7a6
796 @end example 796 @end example
797 797
798 @cindex numeric prefix 798 @cindex numeric prefix
799 @cindex field width 799 @cindex field width
800 @cindex padding 800 @cindex padding
801 All the specification characters allow an optional numeric prefix 801 All the specification characters allow an optional ``width'', which
802 between the @samp{%} and the character. The optional numeric prefix 802 is a digit-string between the @samp{%} and the character. If the
803 defines the minimum width for the object. If the printed 803 printed representation of the object contains fewer characters than
804 representation of the object contains fewer characters than this, then 804 this width, then it is padded. The padding is on the left if the
805 it is padded. The padding is on the left if the prefix is positive 805 prefix is positive (or starts with zero) and on the right if the
806 (or starts with zero) and on the right if the prefix is negative. The 806 prefix is negative. The padding character is normally a space, but if
807 padding character is normally a space, but if the numeric prefix 807 the width starts with a zero, zeros are used for padding. Some of
808 starts with a zero, zeros are used for padding. Some of these 808 these conventions are ignored for specification characters for which
809 conventions are ignored for specification characters for which they do 809 they do not make sense. That is, %s, %S and %c accept a width
810 not make sense. That is, %s, %S and %c accept a numeric prefix
811 starting with 0, but still pad with @emph{spaces} on the left. Also, 810 starting with 0, but still pad with @emph{spaces} on the left. Also,
812 %% accepts a numeric prefix, but ignores it. Here are some examples 811 %% accepts a width, but ignores it. Here are some examples of
813 of padding: 812 padding:
814 813
815 @example 814 @example
816 (format "%06d is padded on the left with zeros" 123) 815 (format "%06d is padded on the left with zeros" 123)
817 @result{} "000123 is padded on the left with zeros" 816 @result{} "000123 is padded on the left with zeros"
818 817
819 (format "%-6d is padded on the right" 123) 818 (format "%-6d is padded on the right" 123)
820 @result{} "123 is padded on the right" 819 @result{} "123 is padded on the right"
821 @end example 820 @end example
822 821
823 @code{format} never truncates an object's printed representation, no 822 If the width is too small, @code{format} does not truncate the
824 matter what width you specify. Thus, you can use a numeric prefix to 823 object's printed representation. Thus, you can use a width to specify
825 specify a minimum spacing between columns with no risk of losing 824 a minimum spacing between columns with no risk of losing information.
826 information.
827 825
828 In the following three examples, @samp{%7s} specifies a minimum width 826 In the following three examples, @samp{%7s} specifies a minimum width
829 of 7. In the first case, the string inserted in place of @samp{%7s} has 827 of 7. In the first case, the string inserted in place of @samp{%7s} has
830 only 3 letters, so 4 blank spaces are inserted for padding. In the 828 only 3 letters, so 4 blank spaces are inserted for padding. In the
831 second case, the string @code{"specification"} is 13 letters wide but is 829 second case, the string @code{"specification"} is 13 letters wide but is
848 (format "The word `%-7s' actually has %d letters in it." 846 (format "The word `%-7s' actually has %d letters in it."
849 "foo" (length "foo")) 847 "foo" (length "foo"))
850 @result{} "The word `foo ' actually has 3 letters in it." 848 @result{} "The word `foo ' actually has 3 letters in it."
851 @end group 849 @end group
852 @end smallexample 850 @end smallexample
851
852 All the specification characters allow an optional ``precision''
853 before the character (after the width, if present). The precision is
854 a decimal-point @samp{.} followed by a digit-string. For the
855 floating-point specifications (%e, %f, %g), the precision specifies
856 how many decimal places to show; if zero, the decimal-point itself is
857 also omitted. For %s and %S, the precision truncates the string to
858 the given width, so @code{"%.3s"} shows only the first three
859 characters of the representation for @var{object}. Precision is
860 ignored for other specification characters.
861
862 Immediately after the % and before the optional width and precision,
863 you can put certain ``flag'' characters.
864
865 A space @var{" "} inserts a space for positive numbers (otherwise
866 nothing is inserted for positive numbers). This flag is ignored
867 except for %d, %e, %f, %g.
868
869 The flag @var{"#"} indicates ``alternate form''. For %o it ensures
870 that the result begins with a 0. For %x and %X the result is prefixed
871 with ``0x'' or ``0X''. For %e, %f, and %g a decimal point is always
872 shown even if the precision is zero.
853 873
854 @node Case Conversion 874 @node Case Conversion
855 @comment node-name, next, previous, up 875 @comment node-name, next, previous, up
856 @section Case Conversion in Lisp 876 @section Case Conversion in Lisp
857 @cindex upper case 877 @cindex upper case