• No results found

Numeric formatting convention inquiry

In document Programming languages — C (Page 178-183)

7.11 Localization <locale.h>

7.11.2 Numeric formatting convention inquiry

7.11.2.1 Thelocaleconvfunction Synopsis

1 #include <locale.h>

struct lconv *localeconv(void);

Description

2 Thelocaleconvfunction sets the components of an object with typestruct lconvwith values appropriate for the formatting of numeric quantities (monetary and otherwise) according to the rules of the current locale.

229)The only functions in 7.4 whose behavior is not affected by the current locale areisdigitandisxdigit.

230)The implementation is thus required to arrange to encode in a string the various categories due to a heterogeneous locale whencategoryhas the valueLC_ALL.

ISO/IEC 9899:20172x::(E) diff:::::::::marks— November 6, 2018 N2310

3 The members of the structure with type char *are pointers to strings, any of which (except decimal_point) can point to"", to indicate that the value is not available in the current locale or is of zero length. Apart fromgroupingandmon_grouping, the strings shall start and end in the initial shift state. The members with typecharare nonnegative numbers, any of which can beCHAR_MAX to indicate that the value is not available in the current locale. The members include the following:

char *decimal_point

The decimal-point character used to format nonmonetary quantities.

char *thousands_sep

The character used to separate groups of digits before the decimal-point character in formatted nonmonetary quantities.

char *grouping

A string whose elements indicate the size of each group of digits in formatted nonmon-etary quantities.

char *mon_decimal_point

The decimal-point used to format monetary quantities.

char *mon_thousands_sep

The separator for groups of digits before the decimal-point in formatted monetary quantities.

char *mon_grouping

A string whose elements indicate the size of each group of digits in formatted monetary quantities.

char *positive_sign

The string used to indicate a nonnegative-valued formatted monetary quantity.

char *negative_sign

The string used to indicate a negative-valued formatted monetary quantity.

char *currency_symbol

The local currency symbol applicable to the current locale.

char frac_digits

The number of fractional digits (those after the decimal-point) to be displayed in a locally formatted monetary quantity.

char p_cs_precedes

Set to 1 or 0 if thecurrency_symbolrespectively precedes or succeeds the value for a nonnegative locally formatted monetary quantity.

char n_cs_precedes

Set to 1 or 0 if thecurrency_symbolrespectively precedes or succeeds the value for a negative locally formatted monetary quantity.

char p_sep_by_space

Set to a value indicating the separation of thecurrency_symbol, the sign string, and the value for a nonnegative locally formatted monetary quantity.

char n_sep_by_space

Set to a value indicating the separation of thecurrency_symbol, the sign string, and the value for a negative locally formatted monetary quantity.

char p_sign_posn

Set to a value indicating the positioning of thepositive_signfor a nonnegative locally formatted monetary quantity.

char n_sign_posn

Set to a value indicating the positioning of thenegative_signfor a negative locally formatted monetary quantity.

char *int_curr_symbol

The international currency symbol applicable to the current locale. The first three characters contain the alphabetic international currency symbol in accordance with those specified in ISO 4217. The fourth character (immediately preceding the null character) is the character used to separate the international currency symbol from the monetary quantity.

char int_frac_digits

The number of fractional digits (those after the decimal-point) to be displayed in an internationally formatted monetary quantity.

char int_p_cs_precedes

Set to 1 or 0 if theint_curr_symbolrespectively precedes or succeeds the value for a nonnegative internationally formatted monetary quantity.

char int_n_cs_precedes

Set to 1 or 0 if theint_curr_symbolrespectively precedes or succeeds the value for a negative internationally formatted monetary quantity.

char int_p_sep_by_space

Set to a value indicating the separation of theint_curr_symbol, the sign string, and the value for a nonnegative internationally formatted monetary quantity.

char int_n_sep_by_space

Set to a value indicating the separation of theint_curr_symbol, the sign string, and the value for a negative internationally formatted monetary quantity.

char int_p_sign_posn

Set to a value indicating the positioning of thepositive_sign for a nonnegative internationally formatted monetary quantity.

char int_n_sign_posn

Set to a value indicating the positioning of thenegative_signfor a negative interna-tionally formatted monetary quantity.

4 The elements ofgroupingandmon_groupingare interpreted according to the following:

CHAR_MAX No further grouping is to be performed.

0 The previous element is to be repeatedly used for the remainder of the digits.

other The integer value is the number of digits that compose the current group. The next element is examined to determine the size of the next group of digits before the current group.

5 The values of p_sep_by_space, n_sep_by_space, int_p_sep_by_space, and int_n_sep_by_spaceare interpreted according to the following:

0 No space separates the currency symbol and value.

1 If the currency symbol and sign string are adjacent, a space separates them from the value;

otherwise, a space separates the currency symbol from the value.

2 If the currency symbol and sign string are adjacent, a space separates them; otherwise, a space separates the sign string from the value.

ISO/IEC 9899:20172x::(E) diff:::::::::marks— November 6, 2018 N2310

Forint_p_sep_by_spaceandint_n_sep_by_space, the fourth character ofint_curr_symbolis used instead of a space.

6 The values of p_sign_posn, n_sign_posn, int_p_sign_posn, and int_n_sign_posn are inter-preted according to the following:

0 Parentheses surround the quantity and currency symbol.

1 The sign string precedes the quantity and currency symbol.

2 The sign string succeeds the quantity and currency symbol.

3 The sign string immediately precedes the currency symbol.

4 The sign string immediately succeeds the currency symbol.

7 The implementation shall behave as if no library function calls thelocaleconvfunction.

Returns

8 Thelocaleconvfunction returns a pointer to the filled-in object. The structure pointed to by the return value shall not be modified by the program, but may be overwritten by a subsequent call to thelocaleconvfunction. In addition, calls to thesetlocalefunction with categoriesLC_ALL, LC_MONETARY, orLC_NUMERICmay overwrite the contents of the structure.

9 EXAMPLE 1 The following table illustrates rules which might well be used by four countries to format monetary quantities.

Local format International format

Country Positive Negative Positive Negative

Country1 1.234,56 mk -1.234,56 mk FIM 1.234,56 FIM -1.234,56

Country2 L.1.234 -L.1.234 ITL 1.234 -ITL 1.234

Country3 ƒ 1.234,56 ƒ -1.234,56 NLG 1.234,56 NLG -1.234,56 Country4 SFrs.1,234.56 SFrs.1,234.56C CHF 1,234.56 CHF 1,234.56C

10 For these four countries, the respective values for the monetary members of the structure returned bylocaleconvcould be:

Country1 Country2 Country3 Country4 mon_decimal_point "," "" "," "."

mon_thousands_sep "." "." "." ","

mon_grouping "\3" "\3" "\3" "\3"

positive_sign "" "" "" ""

negative_sign "-" "-" "-" "C"

currency_symbol "mk" "L." "\u0192" "SFrs."

frac_digits 2 0 2 2

p_cs_precedes 0 1 1 1

n_cs_precedes 0 1 1 1

p_sep_by_space 1 0 1 0

n_sep_by_space 1 0 2 0

p_sign_posn 1 1 1 1

n_sign_posn 1 1 4 2

int_curr_symbol "FIM " "ITL " "NLG " "CHF "

int_frac_digits 2 0 2 2

int_p_cs_precedes 1 1 1 1

int_n_cs_precedes 1 1 1 1

int_p_sep_by_space 1 1 1 1

int_n_sep_by_space 2 1 2 1

int_p_sign_posn 1 1 1 1

int_n_sign_posn 4 1 4 2

11 EXAMPLE 2The following table illustrates how thecs_precedes,sep_by_space, andsign_posnmembers affect the formatted value.

p_sep_by_space

p_cs_precedes p_sign_posn 0 1 2

0 0 (1.25$) (1.25 $) (1.25$)

1 +1.25$ +1.25 $ + 1.25$

2 1.25$+ 1.25 $+ 1.25$ +

3 1.25+$ 1.25 +$ 1.25+ $

4 1.25$+ 1.25 $+ 1.25$ +

1 0 ($1.25) ($ 1.25) ($1.25)

1 +$1.25 +$ 1.25 + $1.25

2 $1.25+ $ 1.25+ $1.25 +

3 +$1.25 +$ 1.25 + $1.25

4 $+1.25 $+ 1.25 $ +1.25

ISO/IEC 9899:20172x::(E) diff:::::::::marks— November 6, 2018 N2310

In document Programming languages — C (Page 178-183)