NumberFormat.hx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib.intl;
  23. /**
  24. The `NumberFormat` object is a constructor for objects that enable language sensitive number formatting.
  25. Documentation [NumberFormat](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NumberFormat$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  26. **/
  27. @:native("Intl.NumberFormat")
  28. extern class NumberFormat {
  29. @:overload(function(?locales:Array<String>, ?options:NumberFormatOptions):Void {})
  30. @:pure function new(?locales:String, ?options:NumberFormatOptions);
  31. /**
  32. Getter function that formats a number according to the locale
  33. and formatting options of this `NumberFormat` object.
  34. **/
  35. @:pure function format(number:Float):String;
  36. /**
  37. Returns an `Array` of objects representing the number string in parts
  38. that can be used for custom locale-aware formatting.
  39. **/
  40. @:pure function formatToParts(?number:Float):Array<NumberFormatPart>;
  41. /**
  42. Returns a new object with properties reflecting the locale and collation options
  43. computed during initialization of the object.
  44. **/
  45. @:pure function resolvedOptions():NumberFormatResolvedOption;
  46. /**
  47. Returns an array containing those of the provided locales that are supported
  48. without having to fall back to the runtime's default locale.
  49. **/
  50. @:overload(function(locales:Array<String>, ?options:NumberFormatSupportedLocalesOfOptions):Array<String> {})
  51. @:pure static function supportedLocalesOf(locales:String, ?options:NumberFormatSupportedLocalesOfOptions):Array<String>;
  52. }
  53. typedef NumberFormatOptions = {
  54. /**
  55. The locale matching algorithm to use.
  56. The default is `BestFit`.
  57. For information about this option, see the [Intl page](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#Locale_negotiation).
  58. **/
  59. var ?localeMatcher:LocaleMatcher;
  60. /**
  61. The formatting style to use.
  62. The default is `Decimal`.
  63. **/
  64. var ?style:NumberFormatStyle;
  65. /**
  66. The currency to use in currency formatting. Possible values are the ISO 4217 currency codes,
  67. such as "USD" for the US dollar, "EUR" for the euro, or "CNY" for the Chinese RMB — see the
  68. [Current currency & funds code list](https://www.currency-iso.org/en/home/tables/table-a1.html).
  69. There is no default value; if the style is "currency", the currency property must be provided.
  70. **/
  71. var ?currency:String;
  72. /**
  73. How to display the currency in currency formatting.
  74. The default is `Symbol`.
  75. **/
  76. var ?currencyDisplay:CurrencyDisplay;
  77. /**
  78. Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators.
  79. The default is `true`.
  80. **/
  81. var ?useGrouping:Bool;
  82. /**
  83. The minimum number of integer digits to use.
  84. Possible values are from 1 to 21; the default is 1.
  85. **/
  86. var ?minimumIntegerDigits:Int;
  87. /**
  88. The minimum number of fraction digits to use.
  89. Possible values are from 0 to 20; the default for plain number and percent formatting is 0;
  90. the default for currency formatting is the number of minor unit digits provided by the
  91. [ISO 4217 currency code list](http://www.currency-iso.org/en/home/tables/table-a1.html)
  92. (2 if the list doesn't provide that information).
  93. **/
  94. var ?minimumFractionDigits:Int;
  95. /**
  96. The maximum number of fraction digits to use.
  97. Possible values are from 0 to 20; the default for plain number formatting is the larger of
  98. minimumFractionDigits and 3; the default for currency formatting is the larger of minimumFractionDigits
  99. and the number of minor unit digits provided by the [ISO 4217 currency code list](http://www.currency-iso.org/en/home/tables/table-a1.html)
  100. (2 if the list doesn't provide that information); the default for percent formatting is the larger of
  101. minimumFractionDigits and 0.
  102. **/
  103. var ?maximumFractionDigits:Int;
  104. /**
  105. The minimum number of significant digits to use.
  106. Possible values are from 1 to 21; the default is 1.
  107. **/
  108. var ?minimumSignificantDigits:Int;
  109. /**
  110. The maximum number of significant digits to use.
  111. Possible values are from 1 to 21; the default is 21.
  112. **/
  113. var ?maximumSignificantDigits:Int;
  114. }
  115. typedef NumberFormatResolvedOption = {
  116. /**
  117. The BCP 47 language tag for the locale actually used. If any Unicode extension values were
  118. requested in the input BCP 47 language tag that led to this locale, the key-value pairs that
  119. were requested and are supported for this locale are included in `locale`.
  120. **/
  121. final locale:String;
  122. /**
  123. The value requested using the Unicode extension key `"nu"` or filled in as a default.
  124. **/
  125. final numberingSystem:String;
  126. final style:NumberFormatStyle;
  127. /**
  128. The values provided for these properties in the `options` argument or filled in as defaults.
  129. **/
  130. final useGrouping:String;
  131. final currency:String;
  132. /**
  133. The values provided for these properties in the `options` argument or filled in as defaults.
  134. These properties are only present if `style` is `"currency"`.
  135. **/
  136. final currencyDisplay:String;
  137. final minimumIntegerDigits:Int;
  138. final minimumFractionDigits:Int;
  139. /**
  140. The values provided for these properties in the `options` argument or filled in as defaults.
  141. These properties are present only if neither m`inimumSignificantDigits` nor `maximumSignificantDigits`
  142. was provided in the `options` argument.
  143. **/
  144. final maximumFractionDigits:Int;
  145. final minimumSignificantDigits:Int;
  146. /**
  147. The values provided for these properties in the `options` argument or filled in as defaults.
  148. These properties are present only if at least one of them was provided in the `options` argument.
  149. **/
  150. final maximumSignificantDigits:Int;
  151. }
  152. enum abstract NumberFormatStyle(String) {
  153. /**
  154. plain number formatting
  155. **/
  156. var Decimal = "decimal";
  157. /**
  158. currency formatting
  159. **/
  160. var Currency = "currency";
  161. /**
  162. percent formatting
  163. **/
  164. var Percent = "percent";
  165. }
  166. enum abstract CurrencyDisplay(String) {
  167. /**
  168. To use a localized currency symbol such as €.
  169. **/
  170. var Symbol = "symbol";
  171. /**
  172. To use the ISO currency code.
  173. **/
  174. var Code = "code";
  175. /**
  176. To use a localized currency name such as "dollar".
  177. **/
  178. var Name = "name";
  179. }
  180. typedef NumberFormatPart = {
  181. final type:NumberFormatPartType;
  182. final value:String;
  183. }
  184. enum abstract NumberFormatPartType(String) {
  185. /**
  186. The currency string, such as the symbols "$" and "€" or the name "Dollar", "Euro" depending
  187. on how currencyDisplay is specified.
  188. **/
  189. var Currency = "currency";
  190. /**
  191. The decimal separator string (".").
  192. **/
  193. var Decimal = "decimal";
  194. /**
  195. The fraction number.
  196. **/
  197. var Fraction = "fraction";
  198. /**
  199. The group separator string (",").
  200. **/
  201. var group = "group";
  202. /**
  203. The Infinity string ("∞").
  204. **/
  205. var infinity = "infinity";
  206. /**
  207. The integer number.
  208. **/
  209. var integer = "integer";
  210. /**
  211. Any literal strings or whitespace in the formatted number.
  212. **/
  213. var literal = "literal";
  214. /**
  215. The minus sign string ("-").
  216. **/
  217. var minusSign = "minusSign";
  218. /**
  219. The NaN string ("NaN").
  220. **/
  221. var nan = "nan";
  222. /**
  223. The plus sign string ("+").
  224. **/
  225. var plusSign = "plusSign";
  226. /**
  227. The percent sign string ("%").
  228. **/
  229. var percentSign = "percentSign";
  230. }
  231. typedef NumberFormatSupportedLocalesOfOptions = {
  232. /**
  233. The locale matching algorithm to use.
  234. The default is `BestFit`.
  235. */
  236. var ?localeMatcher:LocaleMatcher;
  237. }