usimplenumberformatter.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // © 2022 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #ifndef __USIMPLENUMBERFORMATTER_H__
  4. #define __USIMPLENUMBERFORMATTER_H__
  5. #include "unicode/utypes.h"
  6. #if !UCONFIG_NO_FORMATTING
  7. #include "unicode/uformattednumber.h"
  8. #include "unicode/unumberoptions.h"
  9. /**
  10. * \file
  11. * \brief C API: Simple number formatting focused on low memory and code size.
  12. *
  13. * These functions render locale-aware number strings but without the bells and whistles found in
  14. * other number formatting APIs such as those in unumberformatter.h, like units and currencies.
  15. *
  16. * Example using C++ helpers:
  17. *
  18. * <pre>
  19. * LocalUSimpleNumberFormatterPointer uformatter(usnumf_openForLocale("de-CH", status));
  20. * LocalUFormattedNumberPointer uresult(unumf_openResult(status));
  21. * usnumf_formatInt64(uformatter.getAlias(), 55, uresult.getAlias(), status);
  22. * assertEquals("",
  23. * u"55",
  24. * ufmtval_getString(unumf_resultAsValue(uresult.getAlias(), status), nullptr, status));
  25. * </pre>
  26. *
  27. * Example using pure C:
  28. *
  29. * <pre>
  30. * UErrorCode ec = U_ZERO_ERROR;
  31. * USimpleNumberFormatter* uformatter = usnumf_openForLocale("bn", &ec);
  32. * USimpleNumber* unumber = usnum_openForInt64(1000007, &ec);
  33. * UFormattedNumber* uresult = unumf_openResult(&ec);
  34. * usnumf_format(uformatter, unumber, uresult, &ec);
  35. * int32_t len;
  36. * const UChar* str = ufmtval_getString(unumf_resultAsValue(uresult, &ec), &len, &ec);
  37. * if (assertSuccess("Formatting end-to-end", &ec)) {
  38. * assertUEquals("Should produce a result in Bangla digits", u"১০,০০,০০৭", str);
  39. * }
  40. * // Cleanup:
  41. * unumf_closeResult(uresult);
  42. * usnum_close(unumber);
  43. * usnumf_close(uformatter);
  44. * </pre>
  45. */
  46. /**
  47. * An explicit sign option for a SimpleNumber.
  48. *
  49. * @stable ICU 73
  50. */
  51. typedef enum USimpleNumberSign {
  52. /**
  53. * Render a plus sign.
  54. *
  55. * @stable ICU 73
  56. */
  57. UNUM_SIMPLE_NUMBER_PLUS_SIGN,
  58. /**
  59. * Render no sign.
  60. *
  61. * @stable ICU 73
  62. */
  63. UNUM_SIMPLE_NUMBER_NO_SIGN,
  64. /**
  65. * Render a minus sign.
  66. *
  67. * @stable ICU 73
  68. */
  69. UNUM_SIMPLE_NUMBER_MINUS_SIGN,
  70. } USimpleNumberSign;
  71. struct USimpleNumber;
  72. /**
  73. * C-compatible version of icu::number::SimpleNumber.
  74. *
  75. * @stable ICU 73
  76. */
  77. typedef struct USimpleNumber USimpleNumber;
  78. struct USimpleNumberFormatter;
  79. /**
  80. * C-compatible version of icu::number::SimpleNumberFormatter.
  81. *
  82. * @stable ICU 73
  83. */
  84. typedef struct USimpleNumberFormatter USimpleNumberFormatter;
  85. /**
  86. * Creates a new USimpleNumber to be formatted with a USimpleNumberFormatter.
  87. *
  88. * @stable ICU 73
  89. */
  90. U_CAPI USimpleNumber* U_EXPORT2
  91. usnum_openForInt64(int64_t value, UErrorCode* ec);
  92. /**
  93. * Overwrites the value in a USimpleNumber to an int64_t.
  94. *
  95. * This can be used to reset the number value after formatting.
  96. *
  97. * @stable ICU 73
  98. */
  99. U_CAPI void U_EXPORT2
  100. usnum_setToInt64(USimpleNumber* unumber, int64_t value, UErrorCode* ec);
  101. /**
  102. * Changes the value of the USimpleNumber by a power of 10.
  103. *
  104. * This function immediately mutates the inner value.
  105. *
  106. * @stable ICU 73
  107. */
  108. U_CAPI void U_EXPORT2
  109. usnum_multiplyByPowerOfTen(USimpleNumber* unumber, int32_t power, UErrorCode* ec);
  110. /**
  111. * Rounds the value currently stored in the USimpleNumber to the given power of 10,
  112. * which can be before or after the decimal separator.
  113. *
  114. * This function does not change minimum integer digits.
  115. *
  116. * @stable ICU 73
  117. */
  118. U_CAPI void U_EXPORT2
  119. usnum_roundTo(USimpleNumber* unumber, int32_t power, UNumberFormatRoundingMode roundingMode, UErrorCode* ec);
  120. /**
  121. * Pads the beginning of the number with zeros up to the given minimum number of integer digits.
  122. *
  123. * @stable ICU 73
  124. */
  125. U_CAPI void U_EXPORT2
  126. usnum_setMinimumIntegerDigits(USimpleNumber* unumber, int32_t minimumIntegerDigits, UErrorCode* ec);
  127. /**
  128. * Pads the end of the number with zeros up to the given minimum number of fraction digits.
  129. *
  130. * @stable ICU 73
  131. */
  132. U_CAPI void U_EXPORT2
  133. usnum_setMinimumFractionDigits(USimpleNumber* unumber, int32_t minimumFractionDigits, UErrorCode* ec);
  134. #ifndef U_HIDE_DRAFT_API
  135. /**
  136. * Sets the number of integer digits to the given amount, truncating if necessary.
  137. *
  138. * @draft ICU 75
  139. */
  140. U_CAPI void U_EXPORT2
  141. usnum_setMaximumIntegerDigits(USimpleNumber* unumber, int32_t maximumIntegerDigits, UErrorCode* ec);
  142. #endif // U_HIDE_DRAFT_API
  143. #ifndef U_HIDE_DEPRECATED_API
  144. /**
  145. * Alias for setMaximumIntegerDigits.
  146. * Will be removed after ICU 75.
  147. *
  148. * @deprecated ICU 75
  149. */
  150. U_CAPI void U_EXPORT2
  151. usnum_truncateStart(USimpleNumber* unumber, int32_t maximumIntegerDigits, UErrorCode* ec);
  152. #endif // U_HIDE_DEPRECATED_API
  153. /**
  154. * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
  155. *
  156. * This setting is applied upon formatting the number.
  157. *
  158. * NOTE: This does not support accounting sign notation.
  159. *
  160. * @stable ICU 73
  161. */
  162. U_CAPI void U_EXPORT2
  163. usnum_setSign(USimpleNumber* unumber, USimpleNumberSign sign, UErrorCode* ec);
  164. /**
  165. * Creates a new USimpleNumberFormatter with all locale defaults.
  166. *
  167. * @stable ICU 73
  168. */
  169. U_CAPI USimpleNumberFormatter* U_EXPORT2
  170. usnumf_openForLocale(const char* locale, UErrorCode* ec);
  171. /**
  172. * Creates a new USimpleNumberFormatter, overriding the grouping strategy.
  173. *
  174. * @stable ICU 73
  175. */
  176. U_CAPI USimpleNumberFormatter* U_EXPORT2
  177. usnumf_openForLocaleAndGroupingStrategy(
  178. const char* locale, UNumberGroupingStrategy groupingStrategy, UErrorCode* ec);
  179. /**
  180. * Formats a number using this SimpleNumberFormatter.
  181. *
  182. * The USimpleNumber is cleared after calling this function. It can be re-used via
  183. * usnum_setToInt64.
  184. *
  185. * @stable ICU 73
  186. */
  187. U_CAPI void U_EXPORT2
  188. usnumf_format(
  189. const USimpleNumberFormatter* uformatter,
  190. USimpleNumber* unumber,
  191. UFormattedNumber* uresult,
  192. UErrorCode* ec);
  193. /**
  194. * Formats an integer using this SimpleNumberFormatter.
  195. *
  196. * For more control over the formatting, use USimpleNumber.
  197. *
  198. * @stable ICU 73
  199. */
  200. U_CAPI void U_EXPORT2
  201. usnumf_formatInt64(
  202. const USimpleNumberFormatter* uformatter,
  203. int64_t value,
  204. UFormattedNumber* uresult,
  205. UErrorCode* ec);
  206. /**
  207. * Frees the memory held by a USimpleNumber.
  208. *
  209. * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber.
  210. *
  211. * @stable ICU 73
  212. */
  213. U_CAPI void U_EXPORT2
  214. usnum_close(USimpleNumber* unumber);
  215. /**
  216. * Frees the memory held by a USimpleNumberFormatter.
  217. *
  218. * @stable ICU 73
  219. */
  220. U_CAPI void U_EXPORT2
  221. usnumf_close(USimpleNumberFormatter* uformatter);
  222. #if U_SHOW_CPLUSPLUS_API
  223. U_NAMESPACE_BEGIN
  224. /**
  225. * \class LocalUSimpleNumberPointer
  226. * "Smart pointer" class; closes a USimpleNumber via usnum_close().
  227. * For most methods see the LocalPointerBase base class.
  228. *
  229. * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber.
  230. * If you use LocalUSimpleNumberPointer, call `.orphan()` when passing to that function.
  231. *
  232. * Usage:
  233. * <pre>
  234. * LocalUSimpleNumberPointer uformatter(usnumf_openForInteger(...));
  235. * // no need to explicitly call usnum_close()
  236. * </pre>
  237. *
  238. * @see LocalPointerBase
  239. * @see LocalPointer
  240. * @stable ICU 73
  241. */
  242. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberPointer, USimpleNumber, usnum_close);
  243. /**
  244. * \class LocalUSimpleNumberFormatterPointer
  245. * "Smart pointer" class; closes a USimpleNumberFormatter via usnumf_close().
  246. * For most methods see the LocalPointerBase base class.
  247. *
  248. * Usage:
  249. * <pre>
  250. * LocalUSimpleNumberFormatterPointer uformatter(usnumf_openForLocale(...));
  251. * // no need to explicitly call usnumf_close()
  252. * </pre>
  253. *
  254. * @see LocalPointerBase
  255. * @see LocalPointer
  256. * @stable ICU 73
  257. */
  258. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberFormatterPointer, USimpleNumberFormatter, usnumf_close);
  259. U_NAMESPACE_END
  260. #endif // U_SHOW_CPLUSPLUS_API
  261. #endif /* #if !UCONFIG_NO_FORMATTING */
  262. #endif //__USIMPLENUMBERFORMATTER_H__