⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_7.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</DL><P>Defining and installing named locales is normally a responsibility ofthe system administrator at your site (or the person who installed theGNU C library).  Some systems may allow users to create locales, butwe don't discuss that here.<P>If your program needs to use something other than the <SAMP>`C'</SAMP> locale,it will be more portable if you use the whatever locale the userspecifies with the environment, rather than trying to specify somenon-standard locale explicitly by name.  Remember, different machinesmight have different sets of locales installed.<P><H2><A NAME="SEC82" HREF="library_toc.html#SEC82" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC82">Numeric Formatting</A></H2><P>When you want to format a number or a currency amount using theconventions of the current locale, you can use the function<CODE>localeconv</CODE> to get the data on how to do it.  The function<CODE>localeconv</CODE> is declared in the header file <TT>`locale.h'</TT>.<A NAME="IDX373"></A><A NAME="IDX374"></A><A NAME="IDX372"></A><P><A NAME="IDX375"></A><U>Function:</U> struct lconv * <B>localeconv</B> <I>(void)</I><P>The <CODE>localeconv</CODE> function returns a pointer to a structure whosecomponents contain information about how numeric and monetary valuesshould be formatted in the current locale.<P>You shouldn't modify the structure or its contents.  The structure mightbe overwritten by subsequent calls to <CODE>localeconv</CODE>, or by calls to<CODE>setlocale</CODE>, but no other function in the library overwrites thisvalue.<P><A NAME="IDX376"></A><U>Data Type:</U> <B>struct lconv</B><P>This is the data type of the value returned by <CODE>localeconv</CODE>.<P>If a member of the structure <CODE>struct lconv</CODE> has type <CODE>char</CODE>,and the value is <CODE>CHAR_MAX</CODE>, it means that the current locale hasno value for that parameter.<P><H3><A NAME="SEC83" HREF="library_toc.html#SEC83" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC83">Generic Numeric Formatting Parameters</A></H3><P>These are the standard members of <CODE>struct lconv</CODE>; there may beothers.<P><DL COMPACT><DT><CODE>char *decimal_point</CODE><DD><DT><CODE>char *mon_decimal_point</CODE><DD>These are the decimal-point separators used in formatting non-monetaryand monetary quantities, respectively.  In the <SAMP>`C'</SAMP> locale, thevalue of <CODE>decimal_point</CODE> is <CODE>"."</CODE>, and the value of<CODE>mon_decimal_point</CODE> is <CODE>""</CODE>.<A NAME="IDX377"></A><P><DT><CODE>char *thousands_sep</CODE><DD><DT><CODE>char *mon_thousands_sep</CODE><DD>These are the separators used to delimit groups of digits to the left ofthe decimal point in formatting non-monetary and monetary quantities,respectively.  In the <SAMP>`C'</SAMP> locale, both members have a value of<CODE>""</CODE> (the empty string).<P><DT><CODE>char *grouping</CODE><DD><DT><CODE>char *mon_grouping</CODE><DD>These are strings that specify how to group the digits to the left ofthe decimal point.  <CODE>grouping</CODE> applies to non-monetary quantitiesand <CODE>mon_grouping</CODE> applies to monetary quantities.  Use either<CODE>thousands_sep</CODE> or <CODE>mon_thousands_sep</CODE> to separate the digitgroups.<A NAME="IDX378"></A><P>Each string is made up of decimal numbers separated by semicolons.Successive numbers (from left to right) give the sizes of successivegroups (from right to left, starting at the decimal point).  The lastnumber in the string is used over and over for all the remaining groups.<P>If the last integer is <CODE>-1</CODE>, it means that there is no moregrouping--or, put another way, any remaining digits form one largegroup without separators.<P>For example, if <CODE>grouping</CODE> is <CODE>"4;3;2"</CODE>, the number<CODE>123456787654321</CODE> should be grouped into <SAMP>`12'</SAMP>, <SAMP>`34'</SAMP>,<SAMP>`56'</SAMP>, <SAMP>`78'</SAMP>, <SAMP>`765'</SAMP>, <SAMP>`4321'</SAMP>.  This uses a group of 4digits at the end, preceded by a group of 3 digits, preceded by groupsof 2 digits (as many as needed).  With a separator of <SAMP>`,'</SAMP>, thenumber would be printed as <SAMP>`12,34,56,78,765,4321'</SAMP>.<P>A value of <CODE>"3"</CODE> indicates repeated groups of three digits, asnormally used in the U.S.<P>In the standard <SAMP>`C'</SAMP> locale, both <CODE>grouping</CODE> and<CODE>mon_grouping</CODE> have a value of <CODE>""</CODE>.  This value specifies nogrouping at all.<P><DT><CODE>char int_frac_digits</CODE><DD><DT><CODE>char frac_digits</CODE><DD>These are small integers indicating how many fractional digits (to theright of the decimal point) should be displayed in a monetary value ininternational and local formats, respectively.  (Most often, bothmembers have the same value.)<P>In the standard <SAMP>`C'</SAMP> locale, both of these members have the value<CODE>CHAR_MAX</CODE>, meaning "unspecified".  The ANSI standard doesn't saywhat to do when you find this the value; we recommend printing nofractional digits.  (This locale also specifies the empty string for<CODE>mon_decimal_point</CODE>, so printing any fractional digits would beconfusing!)</DL><P><A NAME="IDX379"></A><H3><A NAME="SEC84" HREF="library_toc.html#SEC84" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC84">Printing the Currency Symbol</A></H3><P>These members of the <CODE>struct lconv</CODE> structure specify how to printthe symbol to identify a monetary value--the international analog of<SAMP>`$'</SAMP> for US dollars.<P>Each country has two standard currency symbols.  The <DFN>local currencysymbol</DFN> is used commonly within the country, while the<DFN>international currency symbol</DFN> is used internationally to refer tothat country's currency when it is necessary to indicate the countryunambiguously.<P>For example, many countries use the dollar as their monetary unit, andwhen dealing with international currencies it's important to specifythat one is dealing with (say) Canadian dollars instead of U.S. dollarsor Australian dollars.  But when the context is known to be Canada,there is no need to make this explicit--dollar amounts are implicitlyassumed to be in Canadian dollars.<P><DL COMPACT><DT><CODE>char *currency_symbol</CODE><DD>The local currency symbol for the selected locale.<P>In the standard <SAMP>`C'</SAMP> locale, this member has a value of <CODE>""</CODE>(the empty string), meaning "unspecified".  The ANSI standard doesn'tsay what to do when you find this value; we recommend you simply printthe empty string as you would print any other string found in theappropriate member.<P><DT><CODE>char *int_curr_symbol</CODE><DD>The international currency symbol for the selected locale.<P>The value of <CODE>int_curr_symbol</CODE> should normally consist of athree-letter abbreviation determined by the international standard<CITE>ISO 4217 Codes for the Representation of Currency and Funds</CITE>,followed by a one-character separator (often a space).<P>In the standard <SAMP>`C'</SAMP> locale, this member has a value of <CODE>""</CODE>(the empty string), meaning "unspecified".  We recommend you simplyprint the empty string as you would print any other string found in theappropriate member.<P><DT><CODE>char p_cs_precedes</CODE><DD><DT><CODE>char n_cs_precedes</CODE><DD>These members are <CODE>1</CODE> if the <CODE>currency_symbol</CODE> string shouldprecede the value of a monetary amount, or <CODE>0</CODE> if the string shouldfollow the value.  The <CODE>p_cs_precedes</CODE> member applies to positiveamounts (or zero), and the <CODE>n_cs_precedes</CODE> member applies tonegative amounts.<P>In the standard <SAMP>`C'</SAMP> locale, both of these members have a value of<CODE>CHAR_MAX</CODE>, meaning "unspecified".  The ANSI standard doesn't saywhat to do when you find this value, but we recommend printing thecurrency symbol before the amount.  That's right for most countries.In other words, treat all nonzero values alike in these members.<P>The POSIX standard says that these two members apply to the<CODE>int_curr_symbol</CODE> as well as the <CODE>currency_symbol</CODE>.  The ANSIC standard seems to imply that they should apply only to the<CODE>currency_symbol</CODE>---so the <CODE>int_curr_symbol</CODE> should alwayspreceed the amount.<P>We can only guess which of these (if either) matches the usualconventions for printing international currency symbols.  Our guess isthat they should always preceed the amount.  If we find out a reliableanswer, we will put it here.<P><DT><CODE>char p_sep_by_space</CODE><DD><DT><CODE>char n_sep_by_space</CODE><DD>These members are <CODE>1</CODE> if a space should appear between the<CODE>currency_symbol</CODE> string and the amount, or <CODE>0</CODE> if no spaceshould appear.  The <CODE>p_sep_by_space</CODE> member applies to positiveamounts (or zero), and the <CODE>n_sep_by_space</CODE> member applies tonegative amounts.<P>In the standard <SAMP>`C'</SAMP> locale, both of these members have a value of<CODE>CHAR_MAX</CODE>, meaning "unspecified".  The ANSI standard doesn't saywhat you should do when you find this value; we suggest you treat it asone (print a space).  In other words, treat all nonzero values alike inthese members.<P>These members apply only to <CODE>currency_symbol</CODE>.  When you use<CODE>int_curr_symbol</CODE>, you never print an additional space, because<CODE>int_curr_symbol</CODE> itself contains the appropriate separator.<P>The POSIX standard says that these two members apply to the<CODE>int_curr_symbol</CODE> as well as the <CODE>currency_symbol</CODE>.  But anexample in the ANSI C standard clearly implies that they should applyonly to the <CODE>currency_symbol</CODE>---that the <CODE>int_curr_symbol</CODE>contains any appropriate separator, so you should never print anadditional space.<P>Based on what we know now, we recommend you ignore these members whenprinting international currency symbols, and print no extra space.</DL><P><H3><A NAME="SEC85" HREF="library_toc.html#SEC85" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC85">Printing the Sign of an Amount of Money</A></H3><P>These members of the <CODE>struct lconv</CODE> structure specify how to printthe sign (if any) in a monetary value.<P><DL COMPACT><DT><CODE>char *positive_sign</CODE><DD><DT><CODE>char *negative_sign</CODE><DD>These are strings used to indicate positive (or zero) and negative(respectively) monetary quantities.<P>In the standard <SAMP>`C'</SAMP> locale, both of these members have a value of<CODE>""</CODE> (the empty string), meaning "unspecified".<P>The ANSI standard doesn't say what to do when you find this value; werecommend printing <CODE>positive_sign</CODE> as you find it, even if it isempty.  For a negative value, print <CODE>negative_sign</CODE> as you find itunless both it and <CODE>positive_sign</CODE> are empty, in which case print<SAMP>`-'</SAMP> instead.  (Failing to indicate the sign at all seems ratherunreasonable.)<P><DT><CODE>char p_sign_posn</CODE><DD><DT><CODE>char n_sign_posn</CODE><DD>These members have values that are small integers indicating how toposition the sign for nonnegative and negative monetary quantities,respectively.  (The string used by the sign is what was specified with<CODE>positive_sign</CODE> or <CODE>negative_sign</CODE>.)  The possible values areas follows:<P><DL COMPACT><DT><CODE>0</CODE><DD>The currency symbol and quantity should be surrounded by parentheses.<P><DT><CODE>1</CODE><DD>Print the sign string before the quantity and currency symbol.<P><DT><CODE>2</CODE><DD>Print the sign string after the quantity and currency symbol.<P><DT><CODE>3</CODE><DD>Print the sign string right before the currency symbol.<P><DT><CODE>4</CODE><DD>Print the sign string right after the currency symbol.<P><DT><CODE>CHAR_MAX</CODE><DD>"Unspecified".  Both members have this value in the standard<SAMP>`C'</SAMP> locale.</DL><P>The ANSI standard doesn't say what you should do when the value is<CODE>CHAR_MAX</CODE>.  We recommend you print the sign after the currencysymbol.</DL><P>It is not clear whether you should let these members apply to theinternational currency format or not.  POSIX says you should, butintuition plus the examples in the ANSI C standard suggest you shouldnot.  We hope that someone who knows well the conventions for formattingmonetary quantities will tell us what we should recommend.<P><P>Go to the <A HREF="library_6.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_6.html">previous</A>, <A HREF="library_8.html" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_8.html">next</A> section.<P>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -