Mercurial > emacs
comparison lispref/strings.texi @ 76307:cbba7c4947b7
(Formatting Strings): Clarify width, precision, flags.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Sun, 04 Mar 2007 18:25:36 +0000 |
parents | e303881b06eb |
children | 1eb21d4498cf 91bf6e05918b |
comparison
equal
deleted
inserted
replaced
76306:f64aa3622a9d | 76307:cbba7c4947b7 |
---|---|
819 @end group | 819 @end group |
820 @end example | 820 @end example |
821 | 821 |
822 @cindex field width | 822 @cindex field width |
823 @cindex padding | 823 @cindex padding |
824 All the specification characters allow an optional ``width,'' which | 824 A specification can have a @dfn{width}, which is a signed decimal |
825 is a digit-string between the @samp{%} and the character. If the | 825 number between the @samp{%} and the specification character. If the |
826 printed representation of the object contains fewer characters than | 826 printed representation of the object contains fewer characters than |
827 this width, then it is padded. The padding is on the left if the | 827 this width, @code{format} extends it with padding. The padding goes |
828 width is positive (or starts with zero) and on the right if the | 828 on the left if the width is positive (or starts with zero) and on the |
829 width is negative. The padding character is normally a space, but if | 829 right if the width is negative. The padding character is normally a |
830 the width starts with a zero, zeros are used for padding. Some of | 830 space, but it's @samp{0} if the width starts with a zero. |
831 these conventions are ignored for specification characters for which | 831 |
832 they do not make sense. That is, @samp{%s}, @samp{%S} and @samp{%c} | 832 Some of these conventions are ignored for specification characters |
833 accept a width starting with 0, but still pad with @emph{spaces} on | 833 for which they do not make sense. That is, @samp{%s}, @samp{%S} and |
834 the left. Also, @samp{%%} accepts a width, but ignores it. Here are | 834 @samp{%c} accept a width starting with 0, but still pad with |
835 some examples of padding: | 835 @emph{spaces} on the left. Also, @samp{%%} accepts a width, but |
836 ignores it. Here are some examples of padding: | |
836 | 837 |
837 @example | 838 @example |
838 (format "%06d is padded on the left with zeros" 123) | 839 (format "%06d is padded on the left with zeros" 123) |
839 @result{} "000123 is padded on the left with zeros" | 840 @result{} "000123 is padded on the left with zeros" |
840 | 841 |
841 (format "%-6d is padded on the right" 123) | 842 (format "%-6d is padded on the right" 123) |
842 @result{} "123 is padded on the right" | 843 @result{} "123 is padded on the right" |
843 @end example | 844 @end example |
844 | 845 |
846 @noindent | |
845 If the width is too small, @code{format} does not truncate the | 847 If the width is too small, @code{format} does not truncate the |
846 object's printed representation. Thus, you can use a width to specify | 848 object's printed representation. Thus, you can use a width to specify |
847 a minimum spacing between columns with no risk of losing information. | 849 a minimum spacing between columns with no risk of losing information. |
848 | 850 |
849 In the following three examples, @samp{%7s} specifies a minimum width | 851 In the following three examples, @samp{%7s} specifies a minimum |
850 of 7. In the first case, the string inserted in place of @samp{%7s} has | 852 width of 7. In the first case, the string inserted in place of |
851 only 3 letters, so 4 blank spaces are inserted for padding. In the | 853 @samp{%7s} has only 3 letters, it needs 4 blank spaces as padding. In |
852 second case, the string @code{"specification"} is 13 letters wide but is | 854 the second case, the string @code{"specification"} is 13 letters wide |
853 not truncated. In the third case, the padding is on the right. | 855 but is not truncated. In the third case, the padding is on the right. |
854 | 856 |
855 @smallexample | 857 @smallexample |
856 @group | 858 @group |
857 (format "The word `%7s' actually has %d letters in it." | 859 (format "The word `%7s' actually has %d letters in it." |
858 "foo" (length "foo")) | 860 "foo" (length "foo")) |
871 @result{} "The word `foo ' actually has 3 letters in it." | 873 @result{} "The word `foo ' actually has 3 letters in it." |
872 @end group | 874 @end group |
873 @end smallexample | 875 @end smallexample |
874 | 876 |
875 @cindex precision in format specifications | 877 @cindex precision in format specifications |
876 All the specification characters allow an optional ``precision'' | 878 All the specification characters allow an optional @dfn{precision} |
877 before the character (after the width, if present). The precision is | 879 before the character (after the width, if present). The precision is |
878 a decimal-point @samp{.} followed by a digit-string. For the | 880 a decimal-point @samp{.} followed by a digit-string. For the |
879 floating-point specifications (@samp{%e}, @samp{%f}, @samp{%g}), the | 881 floating-point specifications (@samp{%e}, @samp{%f}, @samp{%g}), the |
880 precision specifies how many decimal places to show; if zero, the | 882 precision specifies how many decimal places to show; if zero, the |
881 decimal-point itself is also omitted. For @samp{%s} and @samp{%S}, | 883 decimal-point itself is also omitted. For @samp{%s} and @samp{%S}, |
882 the precision truncates the string to the given width, so | 884 the precision truncates the string to the given width, so @samp{%.3s} |
883 @samp{%.3s} shows only the first three characters of the | 885 shows only the first three characters of the representation for |
884 representation for @var{object}. Precision is ignored for other | 886 @var{object}. Precision has no effect for other specification |
885 specification characters. | 887 characters. |
886 | 888 |
887 @cindex flags in format specifications | 889 @cindex flags in format specifications |
888 Immediately after the @samp{%} and before the optional width and | 890 Immediately after the @samp{%} and before the optional width and |
889 precision, you can put certain ``flag'' characters. | 891 precision, you can put certain ``flag'' characters. |
890 | 892 |
891 A space character inserts a space for positive numbers, a plus character | 893 @samp{+} as a flag inserts a plus sign before a positive number, so |
892 inserts a plus sign (otherwise nothing is inserted for positive | 894 that it always has a sign. A space character as flag inserts a space |
893 numbers). These flags are ignored except for @samp{%d}, @samp{%e}, | 895 before a positive number. (Otherwise, positive numbers start with the |
894 @samp{%f}, @samp{%g}, and if both flags are present the space is | 896 first digit.) Either of these two flags ensures that positive numbers |
895 ignored. | 897 and negative numbers use the same number of columns. These flags are |
896 | 898 ignored except for @samp{%d}, @samp{%e}, @samp{%f}, @samp{%g}, and if |
897 The flag @samp{#} indicates ``alternate form.'' For @samp{%o} it | 899 both flags are used, the @samp{+} takes precedence. |
898 ensures that the result begins with a 0. For @samp{%x} and @samp{%X} | 900 |
899 the result is prefixed with @samp{0x} or @samp{0X}. For @samp{%e}, | 901 The flag @samp{#} specifies an ``alternate form'' which depends on |
900 @samp{%f}, and @samp{%g} a decimal point is always shown even if the | 902 the format in use. For @samp{%o} it ensures that the result begins |
901 precision is zero. | 903 with a @samp{0}. For @samp{%x} and @samp{%X}, it prefixes the result |
904 with @samp{0x} or @samp{0X}. For @samp{%e}, @samp{%f}, and @samp{%g}, | |
905 the @samp{#} flag means include a decimal point even if the precision | |
906 is zero. | |
902 | 907 |
903 @node Case Conversion | 908 @node Case Conversion |
904 @comment node-name, next, previous, up | 909 @comment node-name, next, previous, up |
905 @section Case Conversion in Lisp | 910 @section Case Conversion in Lisp |
906 @cindex upper case | 911 @cindex upper case |