Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/usimplenumberformatter.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2022 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 
0004 #ifndef __USIMPLENUMBERFORMATTER_H__
0005 #define __USIMPLENUMBERFORMATTER_H__
0006 
0007 #include "unicode/utypes.h"
0008 
0009 #if !UCONFIG_NO_FORMATTING
0010 
0011 #include "unicode/uformattednumber.h"
0012 #include "unicode/unumberoptions.h"
0013 
0014 /**
0015  * \file
0016  * \brief C API: Simple number formatting focused on low memory and code size.
0017  *
0018  * These functions render locale-aware number strings but without the bells and whistles found in
0019  * other number formatting APIs such as those in unumberformatter.h, like units and currencies.
0020  *
0021  * Example using C++ helpers:
0022  *
0023  * <pre>
0024  * LocalUSimpleNumberFormatterPointer uformatter(usnumf_openForLocale("de-CH", status));
0025  * LocalUFormattedNumberPointer uresult(unumf_openResult(status));
0026  * usnumf_formatInt64(uformatter.getAlias(), 55, uresult.getAlias(), status);
0027  * assertEquals("",
0028  *     u"55",
0029  *     ufmtval_getString(unumf_resultAsValue(uresult.getAlias(), status), nullptr, status));
0030  * </pre>
0031  *
0032  * Example using pure C:
0033  * 
0034  * <pre>
0035  * UErrorCode ec = U_ZERO_ERROR;
0036  * USimpleNumberFormatter* uformatter = usnumf_openForLocale("bn", &ec);
0037  * USimpleNumber* unumber = usnum_openForInt64(1000007, &ec);
0038  * UFormattedNumber* uresult = unumf_openResult(&ec);
0039  * usnumf_format(uformatter, unumber, uresult, &ec);
0040  * int32_t len;
0041  * const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), &len, &ec);
0042  * if (assertSuccess("Formatting end-to-end", &ec)) {
0043  *     assertUEquals("Should produce a result in Bangla digits", u"১০,০০,০০৭", str);
0044  * }
0045 
0046  * // Cleanup:
0047  * unumf_closeResult(uresult);
0048  * usnum_close(unumber);
0049  * usnumf_close(uformatter);
0050  * </pre>
0051  */
0052 
0053 #ifndef U_HIDE_DRAFT_API
0054 
0055 
0056 /**
0057  * An explicit sign option for a SimpleNumber.
0058  *
0059  * @draft ICU 73
0060  */
0061 typedef enum USimpleNumberSign {
0062     /**
0063      * Render a plus sign.
0064      *
0065      * @draft ICU 73
0066      */
0067     UNUM_SIMPLE_NUMBER_PLUS_SIGN,
0068     /**
0069      * Render no sign.
0070      *
0071      * @draft ICU 73
0072      */
0073     UNUM_SIMPLE_NUMBER_NO_SIGN,
0074     /**
0075      * Render a minus sign.
0076      *
0077      * @draft ICU 73
0078      */
0079     UNUM_SIMPLE_NUMBER_MINUS_SIGN,
0080 } USimpleNumberSign;
0081 
0082 
0083 struct USimpleNumber;
0084 /**
0085  * C-compatible version of icu::number::SimpleNumber.
0086  *
0087  * @draft ICU 73
0088  */
0089 typedef struct USimpleNumber USimpleNumber;
0090 
0091 
0092 struct USimpleNumberFormatter;
0093 /**
0094  * C-compatible version of icu::number::SimpleNumberFormatter.
0095  *
0096  * @draft ICU 73
0097  */
0098 typedef struct USimpleNumberFormatter USimpleNumberFormatter;
0099 
0100 
0101 /**
0102  * Creates a new USimpleNumber to be formatted with a USimpleNumberFormatter.
0103  *
0104  * @draft ICU 73
0105  */
0106 U_CAPI USimpleNumber* U_EXPORT2
0107 usnum_openForInt64(int64_t value, UErrorCode* ec);
0108 
0109 
0110 /**
0111  * Overwrites the value in a USimpleNumber to an int64_t.
0112  *
0113  * This can be used to reset the number value after formatting.
0114  *
0115  * @draft ICU 73
0116  */
0117 U_CAPI void U_EXPORT2
0118 usnum_setToInt64(USimpleNumber* unumber, int64_t value, UErrorCode* ec);
0119 
0120 
0121 /**
0122  * Changes the value of the USimpleNumber by a power of 10.
0123  *
0124  * This function immediately mutates the inner value.
0125  *
0126  * @draft ICU 73
0127  */
0128 U_CAPI void U_EXPORT2
0129 usnum_multiplyByPowerOfTen(USimpleNumber* unumber, int32_t power, UErrorCode* ec);
0130 
0131 
0132 /**
0133  * Rounds the value currently stored in the USimpleNumber to the given power of 10.
0134  *
0135  * This function immediately mutates the inner value.
0136  *
0137  * @draft ICU 73
0138  */
0139 U_CAPI void U_EXPORT2
0140 usnum_roundTo(USimpleNumber* unumber, int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode* ec);
0141 
0142 
0143 /**
0144  * Pads the beginning of the number with zeros up to the given minimum number of integer digits.
0145  *
0146  * This setting is applied upon formatting the number.
0147  *
0148  * @draft ICU 73
0149  */
0150 U_CAPI void U_EXPORT2
0151 usnum_setMinimumIntegerDigits(USimpleNumber* unumber, int32_t minimumIntegerDigits, UErrorCode* ec);
0152 
0153 
0154 /**
0155  * Pads the end of the number with zeros up to the given minimum number of fraction digits.
0156  *
0157  * This setting is applied upon formatting the number.
0158  *
0159  * @draft ICU 73
0160  */
0161 U_CAPI void U_EXPORT2
0162 usnum_setMinimumFractionDigits(USimpleNumber* unumber, int32_t minimumFractionDigits, UErrorCode* ec);
0163 
0164 
0165 /**
0166  * Truncates digits from the beginning of the number to the given maximum number of integer digits.
0167  *
0168  * This function immediately mutates the inner value.
0169  *
0170  * @draft ICU 73
0171  */
0172 U_CAPI void U_EXPORT2
0173 usnum_truncateStart(USimpleNumber* unumber, int32_t maximumIntegerDigits, UErrorCode* ec);
0174 
0175 
0176 /**
0177  * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
0178  *
0179  * This setting is applied upon formatting the number.
0180  *
0181  * NOTE: This does not support accounting sign notation.
0182  *
0183  * @draft ICU 73
0184  */
0185 U_CAPI void U_EXPORT2
0186 usnum_setSign(USimpleNumber* unumber, USimpleNumberSign sign, UErrorCode* ec);
0187 
0188 
0189 /**
0190  * Creates a new USimpleNumberFormatter with all locale defaults.
0191  *
0192  * @draft ICU 73
0193  */
0194 U_CAPI USimpleNumberFormatter* U_EXPORT2
0195 usnumf_openForLocale(const char* locale, UErrorCode* ec);
0196 
0197 
0198 /**
0199  * Creates a new USimpleNumberFormatter, overriding the grouping strategy.
0200  *
0201  * @draft ICU 73
0202  */
0203 U_CAPI USimpleNumberFormatter* U_EXPORT2
0204 usnumf_openForLocaleAndGroupingStrategy(
0205        const char* locale, UNumberGroupingStrategy groupingStrategy, UErrorCode* ec);
0206 
0207 
0208 /**
0209  * Formats a number using this SimpleNumberFormatter.
0210  *
0211  * The USimpleNumber is cleared after calling this function. It can be re-used via
0212  * usnum_setToInt64.
0213  *
0214  * @draft ICU 73
0215  */
0216 U_CAPI void U_EXPORT2
0217 usnumf_format(
0218     const USimpleNumberFormatter* uformatter,
0219     USimpleNumber* unumber,
0220     UFormattedNumber* uresult,
0221     UErrorCode* ec);
0222 
0223 
0224 /**
0225  * Formats an integer using this SimpleNumberFormatter.
0226  *
0227  * For more control over the formatting, use USimpleNumber.
0228  *
0229  * @draft ICU 73
0230  */
0231 U_CAPI void U_EXPORT2
0232 usnumf_formatInt64(
0233     const USimpleNumberFormatter* uformatter,
0234     int64_t value,
0235     UFormattedNumber* uresult,
0236     UErrorCode* ec);
0237 
0238 
0239 /**
0240  * Frees the memory held by a USimpleNumber.
0241  *
0242  * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber.
0243  *
0244  * @draft ICU 73
0245  */
0246 U_CAPI void U_EXPORT2
0247 usnum_close(USimpleNumber* unumber);
0248 
0249 
0250 /**
0251  * Frees the memory held by a USimpleNumberFormatter.
0252  *
0253  * @draft ICU 73
0254  */
0255 U_CAPI void U_EXPORT2
0256 usnumf_close(USimpleNumberFormatter* uformatter);
0257 
0258 
0259 #if U_SHOW_CPLUSPLUS_API
0260 U_NAMESPACE_BEGIN
0261 
0262 /**
0263  * \class LocalUSimpleNumberPointer
0264  * "Smart pointer" class; closes a USimpleNumber via usnum_close().
0265  * For most methods see the LocalPointerBase base class.
0266  *
0267  * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber.
0268  * If you use LocalUSimpleNumberPointer, call `.orphan()` when passing to that function.
0269  *
0270  * Usage:
0271  * <pre>
0272  * LocalUSimpleNumberPointer uformatter(usnumf_openForInteger(...));
0273  * // no need to explicitly call usnum_close()
0274  * </pre>
0275  *
0276  * @see LocalPointerBase
0277  * @see LocalPointer
0278  * @draft ICU 73
0279  */
0280 U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberPointer, USimpleNumber, usnum_close);
0281 
0282 /**
0283  * \class LocalUSimpleNumberFormatterPointer
0284  * "Smart pointer" class; closes a USimpleNumberFormatter via usnumf_close().
0285  * For most methods see the LocalPointerBase base class.
0286  *
0287  * Usage:
0288  * <pre>
0289  * LocalUSimpleNumberFormatterPointer uformatter(usnumf_openForLocale(...));
0290  * // no need to explicitly call usnumf_close()
0291  * </pre>
0292  *
0293  * @see LocalPointerBase
0294  * @see LocalPointer
0295  * @draft ICU 73
0296  */
0297 U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberFormatterPointer, USimpleNumberFormatter, usnumf_close);
0298 
0299 U_NAMESPACE_END
0300 #endif // U_SHOW_CPLUSPLUS_API
0301 
0302 #endif // U_HIDE_DRAFT_API
0303 
0304 #endif /* #if !UCONFIG_NO_FORMATTING */
0305 #endif //__USIMPLENUMBERFORMATTER_H__