number.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /**
  2. * DevExtreme (localization/globalize/number.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. function _typeof(obj) {
  11. "@babel/helpers - typeof";
  12. return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(obj) {
  13. return typeof obj
  14. } : function(obj) {
  15. return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj
  16. }, _typeof(obj)
  17. }
  18. require("./core");
  19. var Globalize = require("globalize");
  20. var numberLocalization = require("../number");
  21. var errors = require("../../core/errors");
  22. require("globalize/number");
  23. if (Globalize && Globalize.formatNumber) {
  24. var enNumbers = {
  25. main: {
  26. en: {
  27. identity: {
  28. version: {
  29. _cldrVersion: "28",
  30. _number: "$Revision: 11972 $"
  31. },
  32. language: "en"
  33. },
  34. numbers: {
  35. defaultNumberingSystem: "latn",
  36. otherNumberingSystems: {
  37. "native": "latn"
  38. },
  39. minimumGroupingDigits: "1",
  40. "symbols-numberSystem-latn": {
  41. decimal: ".",
  42. group: ",",
  43. list: ";",
  44. percentSign: "%",
  45. plusSign: "+",
  46. minusSign: "-",
  47. exponential: "E",
  48. superscriptingExponent: "\xd7",
  49. perMille: "\u2030",
  50. infinity: "\u221e",
  51. nan: "NaN",
  52. timeSeparator: ":"
  53. },
  54. "decimalFormats-numberSystem-latn": {
  55. standard: "#,##0.###",
  56. "long": {
  57. decimalFormat: {
  58. "1000-count-one": "0 thousand",
  59. "1000-count-other": "0 thousand",
  60. "10000-count-one": "00 thousand",
  61. "10000-count-other": "00 thousand",
  62. "100000-count-one": "000 thousand",
  63. "100000-count-other": "000 thousand",
  64. "1000000-count-one": "0 million",
  65. "1000000-count-other": "0 million",
  66. "10000000-count-one": "00 million",
  67. "10000000-count-other": "00 million",
  68. "100000000-count-one": "000 million",
  69. "100000000-count-other": "000 million",
  70. "1000000000-count-one": "0 billion",
  71. "1000000000-count-other": "0 billion",
  72. "10000000000-count-one": "00 billion",
  73. "10000000000-count-other": "00 billion",
  74. "100000000000-count-one": "000 billion",
  75. "100000000000-count-other": "000 billion",
  76. "1000000000000-count-one": "0 trillion",
  77. "1000000000000-count-other": "0 trillion",
  78. "10000000000000-count-one": "00 trillion",
  79. "10000000000000-count-other": "00 trillion",
  80. "100000000000000-count-one": "000 trillion",
  81. "100000000000000-count-other": "000 trillion"
  82. }
  83. },
  84. "short": {
  85. decimalFormat: {
  86. "1000-count-one": "0K",
  87. "1000-count-other": "0K",
  88. "10000-count-one": "00K",
  89. "10000-count-other": "00K",
  90. "100000-count-one": "000K",
  91. "100000-count-other": "000K",
  92. "1000000-count-one": "0M",
  93. "1000000-count-other": "0M",
  94. "10000000-count-one": "00M",
  95. "10000000-count-other": "00M",
  96. "100000000-count-one": "000M",
  97. "100000000-count-other": "000M",
  98. "1000000000-count-one": "0B",
  99. "1000000000-count-other": "0B",
  100. "10000000000-count-one": "00B",
  101. "10000000000-count-other": "00B",
  102. "100000000000-count-one": "000B",
  103. "100000000000-count-other": "000B",
  104. "1000000000000-count-one": "0T",
  105. "1000000000000-count-other": "0T",
  106. "10000000000000-count-one": "00T",
  107. "10000000000000-count-other": "00T",
  108. "100000000000000-count-one": "000T",
  109. "100000000000000-count-other": "000T"
  110. }
  111. }
  112. },
  113. "scientificFormats-numberSystem-latn": {
  114. standard: "#E0"
  115. },
  116. "percentFormats-numberSystem-latn": {
  117. standard: "#,##0%"
  118. },
  119. "currencyFormats-numberSystem-latn": {
  120. currencySpacing: {
  121. beforeCurrency: {
  122. currencyMatch: "[:^S:]",
  123. surroundingMatch: "[:digit:]",
  124. insertBetween: "\xa0"
  125. },
  126. afterCurrency: {
  127. currencyMatch: "[:^S:]",
  128. surroundingMatch: "[:digit:]",
  129. insertBetween: "\xa0"
  130. }
  131. },
  132. standard: "\xa4#,##0.00",
  133. accounting: "\xa4#,##0.00;(\xa4#,##0.00)",
  134. "short": {
  135. standard: {
  136. "1000-count-one": "\xa40K",
  137. "1000-count-other": "\xa40K",
  138. "10000-count-one": "\xa400K",
  139. "10000-count-other": "\xa400K",
  140. "100000-count-one": "\xa4000K",
  141. "100000-count-other": "\xa4000K",
  142. "1000000-count-one": "\xa40M",
  143. "1000000-count-other": "\xa40M",
  144. "10000000-count-one": "\xa400M",
  145. "10000000-count-other": "\xa400M",
  146. "100000000-count-one": "\xa4000M",
  147. "100000000-count-other": "\xa4000M",
  148. "1000000000-count-one": "\xa40B",
  149. "1000000000-count-other": "\xa40B",
  150. "10000000000-count-one": "\xa400B",
  151. "10000000000-count-other": "\xa400B",
  152. "100000000000-count-one": "\xa4000B",
  153. "100000000000-count-other": "\xa4000B",
  154. "1000000000000-count-one": "\xa40T",
  155. "1000000000000-count-other": "\xa40T",
  156. "10000000000000-count-one": "\xa400T",
  157. "10000000000000-count-other": "\xa400T",
  158. "100000000000000-count-one": "\xa4000T",
  159. "100000000000000-count-other": "\xa4000T"
  160. }
  161. },
  162. "unitPattern-count-one": "{0} {1}",
  163. "unitPattern-count-other": "{0} {1}"
  164. },
  165. "miscPatterns-numberSystem-latn": {
  166. atLeast: "{0}+",
  167. range: "{0}\u2013{1}"
  168. }
  169. }
  170. }
  171. }
  172. };
  173. if ("en" === Globalize.locale().locale) {
  174. Globalize.load(enNumbers);
  175. Globalize.locale("en")
  176. }
  177. var formattersCache = {};
  178. var getFormatter = function(format) {
  179. var formatter;
  180. var formatCacheKey;
  181. if ("object" === _typeof(format)) {
  182. formatCacheKey = Globalize.locale().locale + ":" + JSON.stringify(format)
  183. } else {
  184. formatCacheKey = Globalize.locale().locale + ":" + format
  185. }
  186. formatter = formattersCache[formatCacheKey];
  187. if (!formatter) {
  188. formatter = formattersCache[formatCacheKey] = Globalize.numberFormatter(format)
  189. }
  190. return formatter
  191. };
  192. var globalizeNumberLocalization = {
  193. _formatNumberCore: function(value, format, formatConfig) {
  194. if ("exponential" === format) {
  195. return this.callBase.apply(this, arguments)
  196. }
  197. return getFormatter(this._normalizeFormatConfig(format, formatConfig, value))(value)
  198. },
  199. _normalizeFormatConfig: function(format, formatConfig, value) {
  200. var config;
  201. if ("decimal" === format) {
  202. config = {
  203. minimumIntegerDigits: formatConfig.precision || 1,
  204. useGrouping: false,
  205. minimumFractionDigits: 0,
  206. maximumFractionDigits: 20,
  207. round: value < 0 ? "ceil" : "floor"
  208. }
  209. } else {
  210. config = this._getPrecisionConfig(formatConfig.precision)
  211. }
  212. if ("percent" === format) {
  213. config.style = "percent"
  214. }
  215. return config
  216. },
  217. _getPrecisionConfig: function(precision) {
  218. var config;
  219. if (null === precision) {
  220. config = {
  221. minimumFractionDigits: 0,
  222. maximumFractionDigits: 20
  223. }
  224. } else {
  225. config = {
  226. minimumFractionDigits: precision || 0,
  227. maximumFractionDigits: precision || 0
  228. }
  229. }
  230. return config
  231. },
  232. format: function(value, _format) {
  233. if ("number" !== typeof value) {
  234. return value
  235. }
  236. _format = this._normalizeFormat(_format);
  237. if (!_format || "function" !== typeof _format && !_format.type && !_format.formatter) {
  238. return getFormatter(_format)(value)
  239. }
  240. return this.callBase.apply(this, arguments)
  241. },
  242. parse: function(text, format) {
  243. if (!text) {
  244. return
  245. }
  246. if (format && (format.parser || "string" === typeof format)) {
  247. return this.callBase.apply(this, arguments)
  248. }
  249. if (format) {
  250. errors.log("W0011")
  251. }
  252. var result = Globalize.parseNumber(text);
  253. if (isNaN(result)) {
  254. result = this.callBase.apply(this, arguments)
  255. }
  256. return result
  257. }
  258. };
  259. numberLocalization.inject(globalizeNumberLocalization)
  260. }