|
|
|||
File indexing completed on 2026-04-17 08:35:14
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 /** 0054 * An explicit sign option for a SimpleNumber. 0055 * 0056 * @stable ICU 73 0057 */ 0058 typedef enum USimpleNumberSign { 0059 /** 0060 * Render a plus sign. 0061 * 0062 * @stable ICU 73 0063 */ 0064 UNUM_SIMPLE_NUMBER_PLUS_SIGN, 0065 /** 0066 * Render no sign. 0067 * 0068 * @stable ICU 73 0069 */ 0070 UNUM_SIMPLE_NUMBER_NO_SIGN, 0071 /** 0072 * Render a minus sign. 0073 * 0074 * @stable ICU 73 0075 */ 0076 UNUM_SIMPLE_NUMBER_MINUS_SIGN, 0077 } USimpleNumberSign; 0078 0079 0080 struct USimpleNumber; 0081 /** 0082 * C-compatible version of icu::number::SimpleNumber. 0083 * 0084 * @stable ICU 73 0085 */ 0086 typedef struct USimpleNumber USimpleNumber; 0087 0088 0089 struct USimpleNumberFormatter; 0090 /** 0091 * C-compatible version of icu::number::SimpleNumberFormatter. 0092 * 0093 * @stable ICU 73 0094 */ 0095 typedef struct USimpleNumberFormatter USimpleNumberFormatter; 0096 0097 0098 /** 0099 * Creates a new USimpleNumber to be formatted with a USimpleNumberFormatter. 0100 * 0101 * @stable ICU 73 0102 */ 0103 U_CAPI USimpleNumber* U_EXPORT2 0104 usnum_openForInt64(int64_t value, UErrorCode* ec); 0105 0106 0107 /** 0108 * Overwrites the value in a USimpleNumber to an int64_t. 0109 * 0110 * This can be used to reset the number value after formatting. 0111 * 0112 * @stable ICU 73 0113 */ 0114 U_CAPI void U_EXPORT2 0115 usnum_setToInt64(USimpleNumber* unumber, int64_t value, UErrorCode* ec); 0116 0117 0118 /** 0119 * Changes the value of the USimpleNumber by a power of 10. 0120 * 0121 * This function immediately mutates the inner value. 0122 * 0123 * @stable ICU 73 0124 */ 0125 U_CAPI void U_EXPORT2 0126 usnum_multiplyByPowerOfTen(USimpleNumber* unumber, int32_t power, UErrorCode* ec); 0127 0128 0129 /** 0130 * Rounds the value currently stored in the USimpleNumber to the given power of 10, 0131 * which can be before or after the decimal separator. 0132 * 0133 * This function does not change minimum integer digits. 0134 * 0135 * @stable ICU 73 0136 */ 0137 U_CAPI void U_EXPORT2 0138 usnum_roundTo(USimpleNumber* unumber, int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode* ec); 0139 0140 0141 /** 0142 * Pads the beginning of the number with zeros up to the given minimum number of integer digits. 0143 * 0144 * @stable ICU 73 0145 */ 0146 U_CAPI void U_EXPORT2 0147 usnum_setMinimumIntegerDigits(USimpleNumber* unumber, int32_t minimumIntegerDigits, UErrorCode* ec); 0148 0149 0150 /** 0151 * Pads the end of the number with zeros up to the given minimum number of fraction digits. 0152 * 0153 * @stable ICU 73 0154 */ 0155 U_CAPI void U_EXPORT2 0156 usnum_setMinimumFractionDigits(USimpleNumber* unumber, int32_t minimumFractionDigits, UErrorCode* ec); 0157 0158 0159 #ifndef U_HIDE_DRAFT_API 0160 /** 0161 * Sets the number of integer digits to the given amount, truncating if necessary. 0162 * 0163 * @draft ICU 75 0164 */ 0165 U_CAPI void U_EXPORT2 0166 usnum_setMaximumIntegerDigits(USimpleNumber* unumber, int32_t maximumIntegerDigits, UErrorCode* ec); 0167 #endif // U_HIDE_DRAFT_API 0168 0169 0170 /** 0171 * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign. 0172 * 0173 * This setting is applied upon formatting the number. 0174 * 0175 * NOTE: This does not support accounting sign notation. 0176 * 0177 * @stable ICU 73 0178 */ 0179 U_CAPI void U_EXPORT2 0180 usnum_setSign(USimpleNumber* unumber, USimpleNumberSign sign, UErrorCode* ec); 0181 0182 0183 /** 0184 * Creates a new USimpleNumberFormatter with all locale defaults. 0185 * 0186 * @stable ICU 73 0187 */ 0188 U_CAPI USimpleNumberFormatter* U_EXPORT2 0189 usnumf_openForLocale(const char* locale, UErrorCode* ec); 0190 0191 0192 /** 0193 * Creates a new USimpleNumberFormatter, overriding the grouping strategy. 0194 * 0195 * @stable ICU 73 0196 */ 0197 U_CAPI USimpleNumberFormatter* U_EXPORT2 0198 usnumf_openForLocaleAndGroupingStrategy( 0199 const char* locale, UNumberGroupingStrategy groupingStrategy, UErrorCode* ec); 0200 0201 0202 /** 0203 * Formats a number using this SimpleNumberFormatter. 0204 * 0205 * The USimpleNumber is cleared after calling this function. It can be re-used via 0206 * usnum_setToInt64. 0207 * 0208 * @stable ICU 73 0209 */ 0210 U_CAPI void U_EXPORT2 0211 usnumf_format( 0212 const USimpleNumberFormatter* uformatter, 0213 USimpleNumber* unumber, 0214 UFormattedNumber* uresult, 0215 UErrorCode* ec); 0216 0217 0218 /** 0219 * Formats an integer using this SimpleNumberFormatter. 0220 * 0221 * For more control over the formatting, use USimpleNumber. 0222 * 0223 * @stable ICU 73 0224 */ 0225 U_CAPI void U_EXPORT2 0226 usnumf_formatInt64( 0227 const USimpleNumberFormatter* uformatter, 0228 int64_t value, 0229 UFormattedNumber* uresult, 0230 UErrorCode* ec); 0231 0232 0233 /** 0234 * Frees the memory held by a USimpleNumber. 0235 * 0236 * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber. 0237 * 0238 * @stable ICU 73 0239 */ 0240 U_CAPI void U_EXPORT2 0241 usnum_close(USimpleNumber* unumber); 0242 0243 0244 /** 0245 * Frees the memory held by a USimpleNumberFormatter. 0246 * 0247 * @stable ICU 73 0248 */ 0249 U_CAPI void U_EXPORT2 0250 usnumf_close(USimpleNumberFormatter* uformatter); 0251 0252 0253 #if U_SHOW_CPLUSPLUS_API 0254 U_NAMESPACE_BEGIN 0255 0256 /** 0257 * \class LocalUSimpleNumberPointer 0258 * "Smart pointer" class; closes a USimpleNumber via usnum_close(). 0259 * For most methods see the LocalPointerBase base class. 0260 * 0261 * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber. 0262 * If you use LocalUSimpleNumberPointer, call `.orphan()` when passing to that function. 0263 * 0264 * Usage: 0265 * <pre> 0266 * LocalUSimpleNumberPointer uformatter(usnumf_openForInteger(...)); 0267 * // no need to explicitly call usnum_close() 0268 * </pre> 0269 * 0270 * @see LocalPointerBase 0271 * @see LocalPointer 0272 * @stable ICU 73 0273 */ 0274 U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberPointer, USimpleNumber, usnum_close); 0275 0276 /** 0277 * \class LocalUSimpleNumberFormatterPointer 0278 * "Smart pointer" class; closes a USimpleNumberFormatter via usnumf_close(). 0279 * For most methods see the LocalPointerBase base class. 0280 * 0281 * Usage: 0282 * <pre> 0283 * LocalUSimpleNumberFormatterPointer uformatter(usnumf_openForLocale(...)); 0284 * // no need to explicitly call usnumf_close() 0285 * </pre> 0286 * 0287 * @see LocalPointerBase 0288 * @see LocalPointer 0289 * @stable ICU 73 0290 */ 0291 U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberFormatterPointer, USimpleNumberFormatter, usnumf_close); 0292 0293 U_NAMESPACE_END 0294 #endif // U_SHOW_CPLUSPLUS_API 0295 0296 #endif /* #if !UCONFIG_NO_FORMATTING */ 0297 #endif //__USIMPLENUMBERFORMATTER_H__
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|