621 .sp
622 .LP
623 If an argument operand cannot be completely converted into an internal value
624 appropriate to the corresponding conversion specification, a diagnostic message
625 is written to standard error and the utility does exit with a zero exit status,
626 but continues processing any remaining operands and writes the value
627 accumulated at the time the error was detected to standard output.
628
629 .LP
630 \fBExample 6 \fRAlternative floating point representation 1
631 .sp
632 .LP
633 The \fBprintf\fR utility supports an alternative floating point representation
634 (see \fBprintf\fR(3C) entry for the "\fB%a\fR"/"\fB%A\fR"), which allows the
635 output of floating-point values in a format that avoids the usual base16 to
636 base10 rounding errors.
637
638 .sp
639 .in +2
640 .nf
641 example% printf "%a\n" 2 3.1 NaN
642 .fi
643 .in -2
644 .sp
645
646 .sp
647 .LP
648 produces:
649
650 .sp
651 .in +2
652 .nf
653 0x1.0000000000000000000000000000p+01
654 0x1.8ccccccccccccccccccccccccccdp+01
655 nan
656 .fi
657 .in -2
658 .sp
659
660 .LP
661 \fBExample 7 \fRAlternative floating point representation 2
662 .sp
663 .LP
664 The following example shows two different representations of the same
665 floating-point value.
666
667 .sp
668 .in +2
669 .nf
670 example% x=2 ; printf "%f == %a\n" x x
671 .fi
672 .in -2
673 .sp
674
675 .sp
676 .LP
677 produces:
678
679 .sp
680 .in +2
681 .nf
682 2.000000 == 0x1.0000000000000000000000000000p+01
683 .fi
684 .in -2
685 .sp
686
687 .LP
688 \fBExample 8 \fROutput of unicode values
689 .sp
690 .LP
691 The following command will print the EURO unicode symbol (code-point 0x20ac).
692
693 .sp
694 .in +2
695 .nf
696 example% LC_ALL=en_US.UTF-8 printf "\u[20ac]\n"
697 .fi
698 .in -2
699 .sp
700
701 .sp
702 .LP
703 produces:
704
705 .sp
706 .in +2
707 .nf
708 <euro>
709 .fi
710 .in -2
711 .sp
712
713 .sp
714 .LP
715 where "<euro>" represents the EURO currency symbol character.
716
717 .LP
718 \fBExample 9 \fRConvert unicode character to unicode code-point value
719 .sp
720 .LP
721 The following command will print the hexadecimal value of a given character.
722
723 .sp
724 .in +2
725 .nf
726 example% export LC_ALL=en_US.UTF-8
727 example% printf "%x\n" "'<euro>"
728 .fi
729 .in -2
730 .sp
731
732 .sp
733 .LP
734 where "<euro>" represents the EURO currency symbol character (code-point
735 0x20ac).
736
737 .sp
738 .LP
739 produces:
740
741 .sp
742 .in +2
743 .nf
744 20ac
745 .fi
746 .in -2
747 .sp
748
749 .LP
750 \fBExample 10 \fRPrint the numeric value of an ASCII character
751 .sp
752 .in +2
753 .nf
754 example% printf "%d\n" "'A"
755 .fi
756 .in -2
757 .sp
758
759 .sp
760 .LP
761 produces:
762
763 .sp
764 .in +2
765 .nf
766 65
767 .fi
768 .in -2
769 .sp
770
771 .LP
772 \fBExample 11 \fRPrint the language-independent date and time format
773 .sp
774 .LP
785
786 .sp
787 .LP
788 For example,
789
790 .sp
791 .in +2
792 .nf
793 $ printf format "Sunday" "July" 3 10 2
794 .fi
795 .in -2
796 .sp
797
798 .sp
799 .LP
800 For American usage, format could be the string:
801
802 .sp
803 .in +2
804 .nf
805 "%s, %s %d, %d:%.2d\n"
806 .fi
807 .in -2
808 .sp
809
810 .sp
811 .LP
812 producing the message:
813
814 .sp
815 .in +2
816 .nf
817 Sunday, July 3, 10:02
818 .fi
819 .in -2
820 .sp
821
822 .sp
823 .LP
824 Whereas for EU usage, format could be the string:
825
826 .sp
827 .in +2
828 .nf
829 "%1$s, %3$d. %2$s, %4$d:%5$.2d\n"
830 .fi
831 .in -2
832 .sp
833
834 .sp
835 .LP
836 Note that the '$' characters must be properly escaped, such as
837
838 .sp
839 .in +2
840 .nf
841 "%1\$s, %3\$d. %2\$s, %4\$d:%5\$.2d\n" in this case
842 .fi
843 .in -2
844 .sp
845
846 .sp
847 .LP
848 producing the message:
849
850 .sp
851 .in +2
852 .nf
853 Sunday, 3. July, 10:02
854 .fi
855 .in -2
856 .sp
857
858 .SH ENVIRONMENT VARIABLES
859 .sp
860 .LP
861 See \fBenviron\fR(5) for descriptions of the following environment variables
913 c | c
914 l | l .
915 ATTRIBUTE TYPE ATTRIBUTE VALUE
916 _
917 Interface Stability Uncommitted
918 .TE
919
920 .SH SEE ALSO
921 .sp
922 .LP
923 \fBawk\fR(1), \fBbc\fR(1), \fBdate\fR(1), \fBecho\fR(1), \fBksh93\fR(1),
924 \fBprintf\fR(3C), \fBstrtod\fR(3C), \fBstrtol\fR(3C), \fBstrtoul\fR(3C),
925 \fBattributes\fR(5), \fBenviron\fR(5), \fBformats\fR(5), \fBstandards\fR(5)
926 .SH NOTES
927 .sp
928 .LP
929 Using format specifiers (characters following '%') which are not listed in the
930 \fBprintf\fR(3C) or this manual page will result in undefined behavior.
931 .sp
932 .LP
933 Using escape sequences (the character following a backslash ('\')) which are
934 not listed in the \fBprintf\fR(3C) or this manual page will result in undefined
935 behavior.
936 .sp
937 .LP
938 Floating-point values follow C99, XPG6 and IEEE 754 standard behavior and can
939 handle values the same way as the platform's |\fBlong double\fR| datatype.
940 .sp
941 .LP
942 Floating-point values handle the sign separately which allows signs for values
943 like NaN (for example, -nan), Infinite (for example, -inf) and zero (for
944 example, -0.0).
|
621 .sp
622 .LP
623 If an argument operand cannot be completely converted into an internal value
624 appropriate to the corresponding conversion specification, a diagnostic message
625 is written to standard error and the utility does exit with a zero exit status,
626 but continues processing any remaining operands and writes the value
627 accumulated at the time the error was detected to standard output.
628
629 .LP
630 \fBExample 6 \fRAlternative floating point representation 1
631 .sp
632 .LP
633 The \fBprintf\fR utility supports an alternative floating point representation
634 (see \fBprintf\fR(3C) entry for the "\fB%a\fR"/"\fB%A\fR"), which allows the
635 output of floating-point values in a format that avoids the usual base16 to
636 base10 rounding errors.
637
638 .sp
639 .in +2
640 .nf
641 example% printf "%a\en" 2 3.1 NaN
642 .fi
643 .in -2
644 .sp
645
646 .sp
647 .LP
648 produces:
649
650 .sp
651 .in +2
652 .nf
653 0x1.0000000000000000000000000000p+01
654 0x1.8ccccccccccccccccccccccccccdp+01
655 nan
656 .fi
657 .in -2
658 .sp
659
660 .LP
661 \fBExample 7 \fRAlternative floating point representation 2
662 .sp
663 .LP
664 The following example shows two different representations of the same
665 floating-point value.
666
667 .sp
668 .in +2
669 .nf
670 example% x=2 ; printf "%f == %a\en" x x
671 .fi
672 .in -2
673 .sp
674
675 .sp
676 .LP
677 produces:
678
679 .sp
680 .in +2
681 .nf
682 2.000000 == 0x1.0000000000000000000000000000p+01
683 .fi
684 .in -2
685 .sp
686
687 .LP
688 \fBExample 8 \fROutput of unicode values
689 .sp
690 .LP
691 The following command will print the EURO unicode symbol (code-point 0x20ac).
692
693 .sp
694 .in +2
695 .nf
696 example% LC_ALL=en_US.UTF-8 printf "\u[20ac]\en"
697 .fi
698 .in -2
699 .sp
700
701 .sp
702 .LP
703 produces:
704
705 .sp
706 .in +2
707 .nf
708 <euro>
709 .fi
710 .in -2
711 .sp
712
713 .sp
714 .LP
715 where "<euro>" represents the EURO currency symbol character.
716
717 .LP
718 \fBExample 9 \fRConvert unicode character to unicode code-point value
719 .sp
720 .LP
721 The following command will print the hexadecimal value of a given character.
722
723 .sp
724 .in +2
725 .nf
726 example% export LC_ALL=en_US.UTF-8
727 example% printf "%x\en" "'<euro>"
728 .fi
729 .in -2
730 .sp
731
732 .sp
733 .LP
734 where "<euro>" represents the EURO currency symbol character (code-point
735 0x20ac).
736
737 .sp
738 .LP
739 produces:
740
741 .sp
742 .in +2
743 .nf
744 20ac
745 .fi
746 .in -2
747 .sp
748
749 .LP
750 \fBExample 10 \fRPrint the numeric value of an ASCII character
751 .sp
752 .in +2
753 .nf
754 example% printf "%d\en" "'A"
755 .fi
756 .in -2
757 .sp
758
759 .sp
760 .LP
761 produces:
762
763 .sp
764 .in +2
765 .nf
766 65
767 .fi
768 .in -2
769 .sp
770
771 .LP
772 \fBExample 11 \fRPrint the language-independent date and time format
773 .sp
774 .LP
785
786 .sp
787 .LP
788 For example,
789
790 .sp
791 .in +2
792 .nf
793 $ printf format "Sunday" "July" 3 10 2
794 .fi
795 .in -2
796 .sp
797
798 .sp
799 .LP
800 For American usage, format could be the string:
801
802 .sp
803 .in +2
804 .nf
805 "%s, %s %d, %d:%.2d\en"
806 .fi
807 .in -2
808 .sp
809
810 .sp
811 .LP
812 producing the message:
813
814 .sp
815 .in +2
816 .nf
817 Sunday, July 3, 10:02
818 .fi
819 .in -2
820 .sp
821
822 .sp
823 .LP
824 Whereas for EU usage, format could be the string:
825
826 .sp
827 .in +2
828 .nf
829 "%1$s, %3$d. %2$s, %4$d:%5$.2d\en"
830 .fi
831 .in -2
832 .sp
833
834 .sp
835 .LP
836 Note that the '$' characters must be properly escaped, such as
837
838 .sp
839 .in +2
840 .nf
841 "%1\$s, %3\$d. %2\$s, %4\$d:%5\$.2d\en" in this case
842 .fi
843 .in -2
844 .sp
845
846 .sp
847 .LP
848 producing the message:
849
850 .sp
851 .in +2
852 .nf
853 Sunday, 3. July, 10:02
854 .fi
855 .in -2
856 .sp
857
858 .SH ENVIRONMENT VARIABLES
859 .sp
860 .LP
861 See \fBenviron\fR(5) for descriptions of the following environment variables
913 c | c
914 l | l .
915 ATTRIBUTE TYPE ATTRIBUTE VALUE
916 _
917 Interface Stability Uncommitted
918 .TE
919
920 .SH SEE ALSO
921 .sp
922 .LP
923 \fBawk\fR(1), \fBbc\fR(1), \fBdate\fR(1), \fBecho\fR(1), \fBksh93\fR(1),
924 \fBprintf\fR(3C), \fBstrtod\fR(3C), \fBstrtol\fR(3C), \fBstrtoul\fR(3C),
925 \fBattributes\fR(5), \fBenviron\fR(5), \fBformats\fR(5), \fBstandards\fR(5)
926 .SH NOTES
927 .sp
928 .LP
929 Using format specifiers (characters following '%') which are not listed in the
930 \fBprintf\fR(3C) or this manual page will result in undefined behavior.
931 .sp
932 .LP
933 Using escape sequences (the character following a backslash ('\e')) which are
934 not listed in the \fBprintf\fR(3C) or this manual page will result in undefined
935 behavior.
936 .sp
937 .LP
938 Floating-point values follow C99, XPG6 and IEEE 754 standard behavior and can
939 handle values the same way as the platform's |\fBlong double\fR| datatype.
940 .sp
941 .LP
942 Floating-point values handle the sign separately which allows signs for values
943 like NaN (for example, -nan), Infinite (for example, -inf) and zero (for
944 example, -0.0).
|