33 * // Setup: 34 * UErrorCode ec = U_ZERO_ERROR; 35 * UNumberFormatter* uformatter = unumf_openForSkeletonAndLocale(u"precision-integer", -1, "en", &ec); 36 * UFormattedNumber* uresult = unumf_openResult(&ec); 37 * if (U_FAILURE(ec)) { return; } 38 * 39 * // Format a double: 40 * unumf_formatDouble(uformatter, 5142.3, uresult, &ec); 41 * if (U_FAILURE(ec)) { return; } 42 * 43 * // Export the string to a malloc'd buffer: 44 * int32_t len = unumf_resultToString(uresult, NULL, 0, &ec); 45 * // at this point, ec == U_BUFFER_OVERFLOW_ERROR 46 * ec = U_ZERO_ERROR; 47 * UChar* buffer = (UChar*) malloc((len+1)*sizeof(UChar)); 48 * unumf_resultToString(uresult, buffer, len+1, &ec); 49 * if (U_FAILURE(ec)) { return; } 50 * // buffer should equal "5,142" 51 * 52 * // Cleanup: 53 * unumf_close(uformatter); 54 * unumf_closeResult(uresult); 55 * free(buffer); 56 *
62 * // Setup: 63 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(u"percent", -1, "en", &ec)); 64 * LocalUFormattedNumberPointer uresult(unumf_openResult(&ec)); 65 * if (U_FAILURE(ec)) { return; } 66 * 67 * // Format a decimal number: 68 * unumf_formatDecimal(uformatter.getAlias(), "9.87E-3", -1, uresult.getAlias(), &ec); 69 * if (U_FAILURE(ec)) { return; } 70 * 71 * // Get the location of the percent sign: 72 * UFieldPosition ufpos = {UNUM_PERCENT_FIELD, 0, 0}; 73 * unumf_resultNextFieldPosition(uresult.getAlias(), &ufpos, &ec); 74 * // ufpos should contain beginIndex=7 and endIndex=8 since the string is "0.00987%" 75 * 76 * // No need to do any cleanup since we are using LocalPointer. 77 *
139 *
148 * This enum is similar to {@link UMeasureFormatWidth}. 149 * 150 * @stable ICU 60 151 */ 152 typedef enum UNumberUnitWidth { 153 /** 154 * Print an abbreviated version of the unit name. Similar to SHORT, but always use the shortest available 155 * abbreviation or symbol. This option can be used when the context hints at the identity of the unit. For more 156 * information on the difference between NARROW and SHORT, see SHORT. 157 * 158 *
159 * In CLDR, this option corresponds to the "Narrow" format for measure units and the "¤¤¤¤¤" placeholder for 160 * currencies. 161 * 162 * @stable ICU 60 163 */ 164 UNUM_UNIT_WIDTH_NARROW = 0, 165 166 /** 167 * Print an abbreviated version of the unit name. Similar to NARROW, but use a slightly wider abbreviation or 168 * symbol when there may be ambiguity. This is the default behavior. 169 * 170 *
171 * For example, in es-US, the SHORT form for Fahrenheit is "{0} °F", but the NARROW form is "{0}°", 172 * since Fahrenheit is the customary unit for temperature in that locale. 173 * 174 *
175 * In CLDR, this option corresponds to the "Short" format for measure units and the "¤" placeholder for 176 * currencies. 177 * 178 * @stable ICU 60 179 */ 180 UNUM_UNIT_WIDTH_SHORT = 1, 181 182 /** 183 * Print the full name of the unit, without any abbreviations. 184 * 185 *
186 * In CLDR, this option corresponds to the default format for measure units and the "¤¤¤" placeholder for 187 * currencies. 188 * 189 * @stable ICU 60 190 */ 191 UNUM_UNIT_WIDTH_FULL_NAME = 2, 192 193 /** 194 * Use the three-digit ISO XXX code in place of the symbol for displaying currencies. The behavior of this 195 * option is currently undefined for use with measure units. 196 * 197 *
198 * In CLDR, this option corresponds to the "¤¤" placeholder for currencies. 199 * 200 * @stable ICU 60 201 */ 202 UNUM_UNIT_WIDTH_ISO_CODE = 3, 203 204 /** 205 * Use the formal variant of the currency symbol; for example, "NT$" for the New Taiwan 206 * dollar in zh-TW. 207 * 208 *
209 * Behavior of this option with non-currency units is not defined at this time. 210 * 211 * @stable ICU 68 212 */ 213 UNUM_UNIT_WIDTH_FORMAL = 4, 214 215 /** 216 * Use the alternate variant of the currency symbol; for example, "TL" for the Turkish 217 * lira (TRY). 218 * 219 *
220 * Behavior of this option with non-currency units is not defined at this time. 221 * 222 * @stable ICU 68 223 */ 224 UNUM_UNIT_WIDTH_VARIANT = 5, 225 226 /** 227 * Format the number according to the specified unit, but do not display the unit. For currencies, apply 228 * monetary symbols and formats as with SHORT, but omit the currency symbol. For measure units, the behavior is 229 * equivalent to not specifying the unit at all. 230 * 231 * @stable ICU 60 232 */ 233 UNUM_UNIT_WIDTH_HIDDEN = 6, 234 235 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 236 // needed for unconditionalized struct MacroProps 237 /** 238 * One more than the highest UNumberUnitWidth value. 239 * 240 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 241 */ 242 UNUM_UNIT_WIDTH_COUNT = 7 243 } UNumberUnitWidth; 244 245 /** 246 * An enum declaring how to denote positive and negative numbers. Example outputs when formatting 247 * 123, 0, and -123 in en-US: 248 * 249 *
260 * The exact format, including the position and the code point of the sign, differ by locale. 261 * 262 * @stable ICU 60 263 */ 264 typedef enum UNumberSignDisplay { 265 /** 266 * Show the minus sign on negative numbers, and do not show the sign on positive numbers. This is the default 267 * behavior. 268 * 269 * If using this option, a sign will be displayed on negative zero, including negative numbers 270 * that round to zero. To hide the sign on negative zero, use the NEGATIVE option. 271 * 272 * @stable ICU 60 273 */ 274 UNUM_SIGN_AUTO, 275 276 /** 277 * Show the minus sign on negative numbers and the plus sign on positive numbers, including zero. 278 * To hide the sign on zero, see {@link UNUM_SIGN_EXCEPT_ZERO}. 279 * 280 * @stable ICU 60 281 */ 282 UNUM_SIGN_ALWAYS, 283 284 /** 285 * Do not show the sign on positive or negative numbers. 286 * 287 * @stable ICU 60 288 */ 289 UNUM_SIGN_NEVER, 290 291 /** 292 * Use the locale-dependent accounting format on negative numbers, and do not show the sign on positive numbers. 293 * 294 *
295 * The accounting format is defined in CLDR and varies by locale; in many Western locales, the format is a pair 296 * of parentheses around the number. 297 * 298 *
299 * Note: Since CLDR defines the accounting format in the monetary context only, this option falls back to the 300 * AUTO sign display strategy when formatting without a currency unit. This limitation may be lifted in the 301 * future. 302 * 303 * @stable ICU 60 304 */ 305 UNUM_SIGN_ACCOUNTING, 306 307 /** 308 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on 309 * positive numbers, including zero. For more information on the accounting format, see the 310 * ACCOUNTING sign display strategy. To hide the sign on zero, see 311 * {@link UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO}. 312 * 313 * @stable ICU 60 314 */ 315 UNUM_SIGN_ACCOUNTING_ALWAYS, 316 317 /** 318 * Show the minus sign on negative numbers and the plus sign on positive numbers. Do not show a 319 * sign on zero, numbers that round to zero, or NaN. 320 * 321 * @stable ICU 61 322 */ 323 UNUM_SIGN_EXCEPT_ZERO, 324 325 /** 326 * Use the locale-dependent accounting format on negative numbers, and show the plus sign on 327 * positive numbers. Do not show a sign on zero, numbers that round to zero, or NaN. For more 328 * information on the accounting format, see the ACCOUNTING sign display strategy. 329 * 330 * @stable ICU 61 331 */ 332 UNUM_SIGN_ACCOUNTING_EXCEPT_ZERO, 333 334 /** 335 * Same as AUTO, but do not show the sign on negative zero. 336 * 337 * @stable ICU 69 338 */ 339 UNUM_SIGN_NEGATIVE, 340 341 /** 342 * Same as ACCOUNTING, but do not show the sign on negative zero. 343 * 344 * @stable ICU 69 345 */ 346 UNUM_SIGN_ACCOUNTING_NEGATIVE, 347 348 // Do not conditionalize the following with #ifndef U_HIDE_INTERNAL_API, 349 // needed for unconditionalized struct MacroProps 350 /** 351 * One more than the highest UNumberSignDisplay value. 352 * 353 * @internal ICU 60: The numeric value may change over time; see ICU ticket #12420. 354 */ 355 UNUM_SIGN_COUNT = 9, 356 } UNumberSignDisplay; 357 358 /** 359 * An enum declaring how to render the decimal separator. 360 * 361 *
362 *
562 * LocalUNumberFormatterPointer uformatter(unumf_openForSkeletonAndLocale(...)); 563 * // no need to explicitly call unumf_close() 564 *