usimplenumberformatter.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /**
  144. * Sets the sign of the number: an explicit plus sign, explicit minus sign, or no sign.
  145. *
  146. * This setting is applied upon formatting the number.
  147. *
  148. * NOTE: This does not support accounting sign notation.
  149. *
  150. * @stable ICU 73
  151. */
  152. U_CAPI void U_EXPORT2
  153. usnum_setSign(USimpleNumber* unumber, USimpleNumberSign sign, UErrorCode* ec);
  154. /**
  155. * Creates a new USimpleNumberFormatter with all locale defaults.
  156. *
  157. * @stable ICU 73
  158. */
  159. U_CAPI USimpleNumberFormatter* U_EXPORT2
  160. usnumf_openForLocale(const char* locale, UErrorCode* ec);
  161. /**
  162. * Creates a new USimpleNumberFormatter, overriding the grouping strategy.
  163. *
  164. * @stable ICU 73
  165. */
  166. U_CAPI USimpleNumberFormatter* U_EXPORT2
  167. usnumf_openForLocaleAndGroupingStrategy(
  168. const char* locale, UNumberGroupingStrategy groupingStrategy, UErrorCode* ec);
  169. /**
  170. * Formats a number using this SimpleNumberFormatter.
  171. *
  172. * The USimpleNumber is cleared after calling this function. It can be re-used via
  173. * usnum_setToInt64.
  174. *
  175. * @stable ICU 73
  176. */
  177. U_CAPI void U_EXPORT2
  178. usnumf_format(
  179. const USimpleNumberFormatter* uformatter,
  180. USimpleNumber* unumber,
  181. UFormattedNumber* uresult,
  182. UErrorCode* ec);
  183. /**
  184. * Formats an integer using this SimpleNumberFormatter.
  185. *
  186. * For more control over the formatting, use USimpleNumber.
  187. *
  188. * @stable ICU 73
  189. */
  190. U_CAPI void U_EXPORT2
  191. usnumf_formatInt64(
  192. const USimpleNumberFormatter* uformatter,
  193. int64_t value,
  194. UFormattedNumber* uresult,
  195. UErrorCode* ec);
  196. /**
  197. * Frees the memory held by a USimpleNumber.
  198. *
  199. * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber.
  200. *
  201. * @stable ICU 73
  202. */
  203. U_CAPI void U_EXPORT2
  204. usnum_close(USimpleNumber* unumber);
  205. /**
  206. * Frees the memory held by a USimpleNumberFormatter.
  207. *
  208. * @stable ICU 73
  209. */
  210. U_CAPI void U_EXPORT2
  211. usnumf_close(USimpleNumberFormatter* uformatter);
  212. #if U_SHOW_CPLUSPLUS_API
  213. U_NAMESPACE_BEGIN
  214. /**
  215. * \class LocalUSimpleNumberPointer
  216. * "Smart pointer" class; closes a USimpleNumber via usnum_close().
  217. * For most methods see the LocalPointerBase base class.
  218. *
  219. * NOTE: Normally, a USimpleNumber should be adopted by usnumf_formatAndAdoptNumber.
  220. * If you use LocalUSimpleNumberPointer, call `.orphan()` when passing to that function.
  221. *
  222. * Usage:
  223. * <pre>
  224. * LocalUSimpleNumberPointer uformatter(usnumf_openForInteger(...));
  225. * // no need to explicitly call usnum_close()
  226. * </pre>
  227. *
  228. * @see LocalPointerBase
  229. * @see LocalPointer
  230. * @stable ICU 73
  231. */
  232. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberPointer, USimpleNumber, usnum_close);
  233. /**
  234. * \class LocalUSimpleNumberFormatterPointer
  235. * "Smart pointer" class; closes a USimpleNumberFormatter via usnumf_close().
  236. * For most methods see the LocalPointerBase base class.
  237. *
  238. * Usage:
  239. * <pre>
  240. * LocalUSimpleNumberFormatterPointer uformatter(usnumf_openForLocale(...));
  241. * // no need to explicitly call usnumf_close()
  242. * </pre>
  243. *
  244. * @see LocalPointerBase
  245. * @see LocalPointer
  246. * @stable ICU 73
  247. */
  248. U_DEFINE_LOCAL_OPEN_POINTER(LocalUSimpleNumberFormatterPointer, USimpleNumberFormatter, usnumf_close);
  249. U_NAMESPACE_END
  250. #endif // U_SHOW_CPLUSPLUS_API
  251. #endif /* #if !UCONFIG_NO_FORMATTING */
  252. #endif //__USIMPLENUMBERFORMATTER_H__