numberformat_test.js 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. // Copyright 2006 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. goog.provide('goog.i18n.NumberFormatTest');
  15. goog.setTestOnly('goog.i18n.NumberFormatTest');
  16. goog.require('goog.i18n.CompactNumberFormatSymbols');
  17. goog.require('goog.i18n.CompactNumberFormatSymbols_de');
  18. goog.require('goog.i18n.CompactNumberFormatSymbols_en');
  19. goog.require('goog.i18n.CompactNumberFormatSymbols_fr');
  20. goog.require('goog.i18n.NumberFormat');
  21. goog.require('goog.i18n.NumberFormatSymbols');
  22. goog.require('goog.i18n.NumberFormatSymbols_ar');
  23. goog.require('goog.i18n.NumberFormatSymbols_ar_u_nu_latn');
  24. goog.require('goog.i18n.NumberFormatSymbols_de');
  25. goog.require('goog.i18n.NumberFormatSymbols_en');
  26. goog.require('goog.i18n.NumberFormatSymbols_fr');
  27. goog.require('goog.i18n.NumberFormatSymbols_pl');
  28. goog.require('goog.i18n.NumberFormatSymbols_ro');
  29. goog.require('goog.i18n.NumberFormatSymbols_u_nu_latn');
  30. goog.require('goog.string');
  31. goog.require('goog.testing.ExpectedFailures');
  32. goog.require('goog.testing.PropertyReplacer');
  33. goog.require('goog.testing.jsunit');
  34. goog.require('goog.userAgent');
  35. goog.require('goog.userAgent.product');
  36. goog.require('goog.userAgent.product.isVersion');
  37. var expectedFailures;
  38. var stubs = new goog.testing.PropertyReplacer();
  39. function setUpPage() {
  40. expectedFailures = new goog.testing.ExpectedFailures();
  41. }
  42. function setUp() {
  43. // Always switch back to English on startup.
  44. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
  45. goog.i18n.NumberFormatSymbols_u_nu_latn = goog.i18n.NumberFormatSymbols_en;
  46. goog.i18n.CompactNumberFormatSymbols =
  47. goog.i18n.CompactNumberFormatSymbols_en;
  48. goog.i18n.NumberFormat.setEnforceAsciiDigits(false);
  49. }
  50. function tearDown() {
  51. expectedFailures.handleTearDown();
  52. stubs.reset();
  53. }
  54. function veryBigNumberCompare(str1, str2) {
  55. return str1.length == str2.length &&
  56. str1.substring(0, 8) == str2.substring(0, 8);
  57. }
  58. function testVeryBigNumber() {
  59. var str;
  60. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  61. str = fmt.format(1785599999999999888888888888888);
  62. // when comparing big number, various platform have small different in
  63. // precision. We have to tolerate that using veryBigNumberCompare.
  64. assertTrue(
  65. veryBigNumberCompare(
  66. '$1,785,599,999,999,999,400,000,000,000,000.00', str));
  67. str = fmt.format(1.7856E30);
  68. assertTrue(
  69. veryBigNumberCompare(
  70. '$1,785,599,999,999,999,400,000,000,000,000.00', str));
  71. str = fmt.format(1.3456E20);
  72. assertTrue(veryBigNumberCompare('$134,560,000,000,000,000,000.00', str));
  73. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  74. str = fmt.format(1.3456E20);
  75. assertTrue(veryBigNumberCompare('134,559,999,999,999,980,000', str));
  76. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
  77. str = fmt.format(1.3456E20);
  78. assertTrue(veryBigNumberCompare('13,456,000,000,000,000,000,000%', str));
  79. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.SCIENTIFIC);
  80. str = fmt.format(1.3456E20);
  81. assertEquals('1E20', str);
  82. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  83. str = fmt.format(-1.234567890123456e306);
  84. assertEquals(1 + 1 + 306 + 306 / 3, str.length);
  85. assertEquals('-1,234,567,890,123,45', str.substr(0, 21));
  86. str = fmt.format(Infinity);
  87. assertEquals('\u221e', str);
  88. str = fmt.format(-Infinity);
  89. assertEquals('-\u221e', str);
  90. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  91. str = fmt.format(Infinity);
  92. assertEquals('\u221eT', str);
  93. str = fmt.format(-Infinity);
  94. assertEquals('-\u221eT', str);
  95. }
  96. function testStandardFormat() {
  97. var str;
  98. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  99. str = fmt.format(1234.579);
  100. assertEquals('$1,234.58', str);
  101. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  102. str = fmt.format(1234.579);
  103. assertEquals('1,234.579', str);
  104. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
  105. str = fmt.format(1234.579);
  106. assertEquals('123,458%', str);
  107. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.SCIENTIFIC);
  108. str = fmt.format(1234.579);
  109. assertEquals('1E3', str);
  110. // Math.log(1000000)/Math.LN10 is strictly less than 6. Make sure it gets
  111. // formatted correctly.
  112. str = fmt.format(1000000);
  113. assertEquals('1E6', str);
  114. }
  115. function testNegativePercentage() {
  116. var str;
  117. var fmt = new goog.i18n.NumberFormat('#,##0.00%');
  118. str = fmt.format(-1234.56);
  119. assertEquals('-123,456.00%', str);
  120. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
  121. str = fmt.format(-1234.579);
  122. assertEquals('-123,458%', str);
  123. }
  124. function testNegativePercentagePattern() {
  125. var str;
  126. var fmt = new goog.i18n.NumberFormat('#,##0.00%;(#,##0.00%)');
  127. str = fmt.format(1234.56);
  128. assertEquals('123,456.00%', str);
  129. str = fmt.format(-1234.56);
  130. assertEquals('(123,456.00%)', str);
  131. }
  132. function testCustomPercentage() {
  133. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
  134. fmt.setMaximumFractionDigits(1);
  135. fmt.setMinimumFractionDigits(1);
  136. var str = fmt.format(0.1291);
  137. assertEquals('12.9%', str);
  138. fmt.setMaximumFractionDigits(2);
  139. fmt.setMinimumFractionDigits(1);
  140. str = fmt.format(0.129);
  141. assertEquals('12.9%', str);
  142. fmt.setMaximumFractionDigits(2);
  143. fmt.setMinimumFractionDigits(1);
  144. str = fmt.format(0.12);
  145. assertEquals('12.0%', str);
  146. fmt.setMaximumFractionDigits(2);
  147. fmt.setMinimumFractionDigits(1);
  148. str = fmt.format(0.12911);
  149. assertEquals('12.91%', str);
  150. }
  151. function testBasicParse() {
  152. var value;
  153. var fmt = new goog.i18n.NumberFormat('0.0000');
  154. value = fmt.parse('123.4579');
  155. assertEquals(123.4579, value);
  156. value = fmt.parse('+123.4579');
  157. assertEquals(123.4579, value);
  158. value = fmt.parse('-123.4579');
  159. assertEquals(-123.4579, value);
  160. }
  161. function testPrefixParse() {
  162. var value;
  163. var fmt = new goog.i18n.NumberFormat('0.0;(0.0)');
  164. value = fmt.parse('123.4579');
  165. assertEquals(123.4579, value);
  166. value = fmt.parse('(123.4579)');
  167. assertEquals(-123.4579, value);
  168. }
  169. function testPrecentParse() {
  170. var value;
  171. var fmt = new goog.i18n.NumberFormat('0.0;(0.0)');
  172. value = fmt.parse('123.4579%');
  173. assertEquals((123.4579 / 100), value);
  174. value = fmt.parse('(%123.4579)');
  175. assertEquals((-123.4579 / 100), value);
  176. value = fmt.parse('123.4579\u2030');
  177. assertEquals((123.4579 / 1000), value);
  178. value = fmt.parse('(\u2030123.4579)');
  179. assertEquals((-123.4579 / 1000), value);
  180. }
  181. function testPercentAndPerMillAdvance() {
  182. var value;
  183. var pos = [0];
  184. var fmt = new goog.i18n.NumberFormat('0');
  185. value = fmt.parse('120%', pos);
  186. assertEquals(1.2, value);
  187. assertEquals(4, pos[0]);
  188. pos[0] = 0;
  189. value = fmt.parse('120\u2030', pos);
  190. assertEquals(0.12, value);
  191. assertEquals(4, pos[0]);
  192. }
  193. function testPercentAndPerMillParsing() {
  194. var implicitFmt = new goog.i18n.NumberFormat('0;(0)');
  195. assertEquals(123 / 100, implicitFmt.parse("123%"));
  196. assertEquals(-123 / 100, implicitFmt.parse("(123%)"));
  197. assertEquals(123 / 1000, implicitFmt.parse("123‰"));
  198. assertEquals(-123 / 1000, implicitFmt.parse("(123‰)"));
  199. var explicitFmtPercent = new goog.i18n.NumberFormat('0%;(0%)');
  200. assertEquals(123 / 100, explicitFmtPercent.parse("123%"));
  201. assertEquals(-123 / 100, explicitFmtPercent.parse("(123%)"));
  202. var explicitFmtPermill = new goog.i18n.NumberFormat('0‰;(0‰)');
  203. assertEquals(123 / 1000, explicitFmtPermill.parse("123‰"));
  204. assertEquals(-123 / 1000, explicitFmtPermill.parse("(123‰)"));
  205. }
  206. function testInfinityParse() {
  207. var value;
  208. var fmt = new goog.i18n.NumberFormat('0.0;(0.0)');
  209. // gwt need to add those symbols first
  210. value = fmt.parse('\u221e');
  211. assertEquals(Number.POSITIVE_INFINITY, value);
  212. value = fmt.parse('(\u221e)');
  213. assertEquals(Number.NEGATIVE_INFINITY, value);
  214. }
  215. function testExponentParse() {
  216. var value;
  217. var fmt;
  218. fmt = new goog.i18n.NumberFormat('#E0');
  219. value = fmt.parse('1.234E3');
  220. assertEquals(1.234E+3, value);
  221. fmt = new goog.i18n.NumberFormat('0.###E0');
  222. value = fmt.parse('1.234E3');
  223. assertEquals(1.234E+3, value);
  224. fmt = new goog.i18n.NumberFormat('#E0');
  225. value = fmt.parse('1.2345E4');
  226. assertEquals(12345.0, value);
  227. value = fmt.parse('1.2345E4');
  228. assertEquals(12345.0, value);
  229. value = fmt.parse('1.2345E+4');
  230. assertEquals(12345.0, value);
  231. }
  232. function testGroupingParse() {
  233. var value;
  234. var fmt = new goog.i18n.NumberFormat('#,###');
  235. value = fmt.parse('1,234,567,890');
  236. assertEquals(1234567890, value);
  237. value = fmt.parse('12,3456,7890');
  238. assertEquals(1234567890, value);
  239. fmt = new goog.i18n.NumberFormat('#');
  240. value = fmt.parse('1234567890');
  241. assertEquals(1234567890, value);
  242. }
  243. function testParsingStop() {
  244. var pos = [0];
  245. var fmt = new goog.i18n.NumberFormat('###0.###E0');
  246. assertEquals(123.457, fmt.parse('123.457', pos));
  247. assertEquals(7, pos[0]);
  248. pos[0] = 0;
  249. assertEquals(123.457, fmt.parse('+123.457', pos));
  250. assertEquals(8, pos[0]);
  251. pos[0] = 0;
  252. assertEquals(123, fmt.parse('123 cars in the parking lot.', pos));
  253. assertEquals(3, pos[0]);
  254. pos[0] = 0;
  255. assertEquals(12, fmt.parse('12 + 12', pos));
  256. assertEquals(2, pos[0]);
  257. pos[0] = 0;
  258. assertEquals(12, fmt.parse('12+12', pos));
  259. assertEquals(2, pos[0]);
  260. pos[0] = 0;
  261. assertEquals(120, fmt.parse('1.2E+2', pos));
  262. assertEquals(6, pos[0]);
  263. pos[0] = 0;
  264. assertEquals(120, fmt.parse('1.2E+2-12', pos));
  265. assertEquals(6, pos[0]);
  266. }
  267. function testBasicFormat() {
  268. var fmt = new goog.i18n.NumberFormat('0.0000');
  269. var str = fmt.format(123.45789179565757);
  270. assertEquals('123.4579', str);
  271. }
  272. function testGrouping() {
  273. var str;
  274. var fmt = new goog.i18n.NumberFormat('#,###');
  275. str = fmt.format(1234567890);
  276. assertEquals('1,234,567,890', str);
  277. fmt = new goog.i18n.NumberFormat('#,####');
  278. str = fmt.format(1234567890);
  279. assertEquals('12,3456,7890', str);
  280. fmt = new goog.i18n.NumberFormat('#');
  281. str = fmt.format(1234567890);
  282. assertEquals('1234567890', str);
  283. }
  284. function testIndiaNumberGrouping() {
  285. // Test for a known grouping used and recognized in India
  286. var fmt = new goog.i18n.NumberFormat('#,##,###');
  287. var str = fmt.format(1);
  288. assertEquals('1', str);
  289. str = fmt.format(12);
  290. assertEquals('12', str);
  291. str = fmt.format(123);
  292. assertEquals('123', str);
  293. str = fmt.format(1234);
  294. assertEquals('1,234', str);
  295. str = fmt.format(12345);
  296. assertEquals('12,345', str);
  297. str = fmt.format(123456);
  298. assertEquals('1,23,456', str);
  299. str = fmt.format(1234567);
  300. assertEquals('12,34,567', str);
  301. str = fmt.format(12345678);
  302. assertEquals('1,23,45,678', str);
  303. str = fmt.format(123456789);
  304. assertEquals('12,34,56,789', str);
  305. str = fmt.format(1234567890);
  306. assertEquals('1,23,45,67,890', str);
  307. str = fmt.format(0);
  308. assertEquals('0', str);
  309. str = fmt.format(-1);
  310. assertEquals('-1', str);
  311. str = fmt.format(-12);
  312. assertEquals('-12', str);
  313. str = fmt.format(-123);
  314. assertEquals('-123', str);
  315. str = fmt.format(-1234);
  316. assertEquals('-1,234', str);
  317. str = fmt.format(-12345);
  318. assertEquals('-12,345', str);
  319. str = fmt.format(-123456);
  320. assertEquals('-1,23,456', str);
  321. str = fmt.format(-1234567);
  322. assertEquals('-12,34,567', str);
  323. str = fmt.format(-12345678);
  324. assertEquals('-1,23,45,678', str);
  325. str = fmt.format(-123456789);
  326. assertEquals('-12,34,56,789', str);
  327. str = fmt.format(-1234567890);
  328. assertEquals('-1,23,45,67,890', str);
  329. }
  330. function testUnknownNumberGroupings() {
  331. // Test for any future unknown grouping format in addition to India
  332. var fmt = new goog.i18n.NumberFormat('#,####,##,###');
  333. var str = fmt.format(1);
  334. assertEquals('1', str);
  335. str = fmt.format(12);
  336. assertEquals('12', str);
  337. str = fmt.format(123);
  338. assertEquals('123', str);
  339. str = fmt.format(1234);
  340. assertEquals('1,234', str);
  341. str = fmt.format(12345);
  342. assertEquals('12,345', str);
  343. str = fmt.format(123456);
  344. assertEquals('1,23,456', str);
  345. str = fmt.format(1234567);
  346. assertEquals('12,34,567', str);
  347. str = fmt.format(12345678);
  348. assertEquals('123,45,678', str);
  349. str = fmt.format(123456789);
  350. assertEquals('1234,56,789', str);
  351. str = fmt.format(1234567890);
  352. assertEquals('1,2345,67,890', str);
  353. str = fmt.format(11234567890);
  354. assertEquals('11,2345,67,890', str);
  355. str = fmt.format(111234567890);
  356. assertEquals('111,2345,67,890', str);
  357. str = fmt.format(1111234567890);
  358. assertEquals('1111,2345,67,890', str);
  359. str = fmt.format(11111234567890);
  360. assertEquals('1,1111,2345,67,890', str);
  361. str = fmt.format(0);
  362. assertEquals('0', str);
  363. str = fmt.format(-1);
  364. assertEquals('-1', str);
  365. str = fmt.format(-12);
  366. assertEquals('-12', str);
  367. str = fmt.format(-123);
  368. assertEquals('-123', str);
  369. str = fmt.format(-1234);
  370. assertEquals('-1,234', str);
  371. str = fmt.format(-12345);
  372. assertEquals('-12,345', str);
  373. str = fmt.format(-123456);
  374. assertEquals('-1,23,456', str);
  375. str = fmt.format(-1234567);
  376. assertEquals('-12,34,567', str);
  377. str = fmt.format(-12345678);
  378. assertEquals('-123,45,678', str);
  379. str = fmt.format(-123456789);
  380. assertEquals('-1234,56,789', str);
  381. str = fmt.format(-1234567890);
  382. assertEquals('-1,2345,67,890', str);
  383. str = fmt.format(-11234567890);
  384. assertEquals('-11,2345,67,890', str);
  385. str = fmt.format(-111234567890);
  386. assertEquals('-111,2345,67,890', str);
  387. str = fmt.format(-1111234567890);
  388. assertEquals('-1111,2345,67,890', str);
  389. str = fmt.format(-11111234567890);
  390. assertEquals('-1,1111,2345,67,890', str);
  391. fmt = new goog.i18n.NumberFormat('#,#,##,###,#');
  392. str = fmt.format(1);
  393. assertEquals('1', str);
  394. str = fmt.format(12);
  395. assertEquals('1,2', str);
  396. str = fmt.format(123);
  397. assertEquals('12,3', str);
  398. str = fmt.format(1234);
  399. assertEquals('123,4', str);
  400. str = fmt.format(12345);
  401. assertEquals('1,234,5', str);
  402. str = fmt.format(123456);
  403. assertEquals('12,345,6', str);
  404. str = fmt.format(1234567);
  405. assertEquals('1,23,456,7', str);
  406. str = fmt.format(12345678);
  407. assertEquals('1,2,34,567,8', str);
  408. str = fmt.format(123456789);
  409. assertEquals('1,2,3,45,678,9', str);
  410. str = fmt.format(1234567890);
  411. assertEquals('1,2,3,4,56,789,0', str);
  412. str = fmt.format(0);
  413. assertEquals('0', str);
  414. str = fmt.format(-1);
  415. assertEquals('-1', str);
  416. str = fmt.format(-12);
  417. assertEquals('-1,2', str);
  418. str = fmt.format(-123);
  419. assertEquals('-12,3', str);
  420. str = fmt.format(-1234);
  421. assertEquals('-123,4', str);
  422. str = fmt.format(-12345);
  423. assertEquals('-1,234,5', str);
  424. str = fmt.format(-123456);
  425. assertEquals('-12,345,6', str);
  426. str = fmt.format(-1234567);
  427. assertEquals('-1,23,456,7', str);
  428. str = fmt.format(-12345678);
  429. assertEquals('-1,2,34,567,8', str);
  430. str = fmt.format(-123456789);
  431. assertEquals('-1,2,3,45,678,9', str);
  432. str = fmt.format(-1234567890);
  433. assertEquals('-1,2,3,4,56,789,0', str);
  434. }
  435. function testPerMill() {
  436. var str;
  437. var fmt = new goog.i18n.NumberFormat('###.###\u2030');
  438. str = fmt.format(0.4857);
  439. assertEquals('485.7\u2030', str);
  440. }
  441. function testCurrency() {
  442. var str;
  443. var fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00');
  444. str = fmt.format(1234.56);
  445. assertEquals('$1,234.56', str);
  446. str = fmt.format(-1234.56);
  447. assertEquals('-$1,234.56', str);
  448. fmt = new goog.i18n.NumberFormat(
  449. '\u00a4#,##0.00;-\u00a4#,##0.00', 'USD',
  450. goog.i18n.NumberFormat.CurrencyStyle.LOCAL);
  451. str = fmt.format(1234.56);
  452. assertEquals('$1,234.56', str);
  453. str = fmt.format(-1234.56);
  454. assertEquals('-$1,234.56', str);
  455. fmt = new goog.i18n.NumberFormat(
  456. '\u00a4#,##0.00;-\u00a4#,##0.00', 'USD',
  457. goog.i18n.NumberFormat.CurrencyStyle.PORTABLE);
  458. str = fmt.format(1234.56);
  459. assertEquals('US$1,234.56', str);
  460. str = fmt.format(-1234.56);
  461. assertEquals('-US$1,234.56', str);
  462. fmt = new goog.i18n.NumberFormat(
  463. '\u00a4#,##0.00;-\u00a4#,##0.00', 'USD',
  464. goog.i18n.NumberFormat.CurrencyStyle.GLOBAL);
  465. str = fmt.format(1234.56);
  466. assertEquals('USD $1,234.56', str);
  467. str = fmt.format(-1234.56);
  468. assertEquals('-USD $1,234.56', str);
  469. fmt = new goog.i18n.NumberFormat(
  470. '\u00a4\u00a4 #,##0.00;-\u00a4\u00a4 #,##0.00');
  471. str = fmt.format(1234.56);
  472. assertEquals('USD 1,234.56', str);
  473. fmt = new goog.i18n.NumberFormat(
  474. '\u00a4\u00a4 #,##0.00;\u00a4\u00a4 -#,##0.00');
  475. str = fmt.format(-1234.56);
  476. assertEquals('USD -1,234.56', str);
  477. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00', 'BRL');
  478. str = fmt.format(1234.56);
  479. assertEquals('R$1,234.56', str);
  480. str = fmt.format(-1234.56);
  481. assertEquals('-R$1,234.56', str);
  482. fmt = new goog.i18n.NumberFormat(
  483. '\u00a4\u00a4 #,##0.00;(\u00a4\u00a4 #,##0.00)', 'BRL');
  484. str = fmt.format(1234.56);
  485. assertEquals('BRL 1,234.56', str);
  486. str = fmt.format(-1234.56);
  487. assertEquals('(BRL 1,234.56)', str);
  488. // Test implicit negative pattern.
  489. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00');
  490. str = fmt.format(1234.56);
  491. assertEquals('$1,234.56', str);
  492. str = fmt.format(-1234.56);
  493. assertEquals('-$1,234.56', str);
  494. }
  495. function testQuotes() {
  496. var str;
  497. var fmt = new goog.i18n.NumberFormat('a\'fo\'\'o\'b#');
  498. str = fmt.format(123);
  499. assertEquals('afo\'ob123', str);
  500. fmt = new goog.i18n.NumberFormat('a\'\'b#');
  501. str = fmt.format(123);
  502. assertEquals('a\'b123', str);
  503. fmt = new goog.i18n.NumberFormat('a\'fo\'\'o\'b#');
  504. str = fmt.format(-123);
  505. assertEquals('-afo\'ob123', str);
  506. fmt = new goog.i18n.NumberFormat('a\'\'b#');
  507. str = fmt.format(-123);
  508. assertEquals('-a\'b123', str);
  509. }
  510. function testZeros() {
  511. var str;
  512. var fmt;
  513. fmt = new goog.i18n.NumberFormat('#.#');
  514. str = fmt.format(0);
  515. assertEquals('0', str);
  516. fmt = new goog.i18n.NumberFormat('#.');
  517. str = fmt.format(0);
  518. assertEquals('0.', str);
  519. fmt = new goog.i18n.NumberFormat('.#');
  520. str = fmt.format(0);
  521. assertEquals('.0', str);
  522. fmt = new goog.i18n.NumberFormat('#');
  523. str = fmt.format(0);
  524. assertEquals('0', str);
  525. fmt = new goog.i18n.NumberFormat('#0.#');
  526. str = fmt.format(0);
  527. assertEquals('0', str);
  528. fmt = new goog.i18n.NumberFormat('#0.');
  529. str = fmt.format(0);
  530. assertEquals('0.', str);
  531. fmt = new goog.i18n.NumberFormat('#.0');
  532. str = fmt.format(0);
  533. assertEquals('.0', str);
  534. fmt = new goog.i18n.NumberFormat('#');
  535. str = fmt.format(0);
  536. assertEquals('0', str);
  537. fmt = new goog.i18n.NumberFormat('000');
  538. str = fmt.format(0);
  539. assertEquals('000', str);
  540. }
  541. function testExponential() {
  542. var str;
  543. var fmt;
  544. fmt = new goog.i18n.NumberFormat('0.####E0');
  545. str = fmt.format(0.01234);
  546. assertEquals('1.234E-2', str);
  547. fmt = new goog.i18n.NumberFormat('00.000E00');
  548. str = fmt.format(0.01234);
  549. assertEquals('12.340E-03', str);
  550. fmt = new goog.i18n.NumberFormat('##0.######E000');
  551. str = fmt.format(0.01234);
  552. assertEquals('12.34E-003', str);
  553. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  554. str = fmt.format(0.01234);
  555. assertEquals('1.234E-2', str);
  556. fmt = new goog.i18n.NumberFormat('0.####E0');
  557. str = fmt.format(123456789);
  558. assertEquals('1.2346E8', str);
  559. fmt = new goog.i18n.NumberFormat('00.000E00');
  560. str = fmt.format(123456789);
  561. assertEquals('12.346E07', str);
  562. fmt = new goog.i18n.NumberFormat('##0.######E000');
  563. str = fmt.format(123456789);
  564. assertEquals('123.456789E006', str);
  565. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  566. str = fmt.format(123456789);
  567. assertEquals('1.235E8', str);
  568. fmt = new goog.i18n.NumberFormat('0.####E0');
  569. str = fmt.format(1.23e300);
  570. assertEquals('1.23E300', str);
  571. fmt = new goog.i18n.NumberFormat('00.000E00');
  572. str = fmt.format(1.23e300);
  573. assertEquals('12.300E299', str);
  574. fmt = new goog.i18n.NumberFormat('##0.######E000');
  575. str = fmt.format(1.23e300);
  576. assertEquals('1.23E300', str);
  577. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  578. str = fmt.format(1.23e300);
  579. assertEquals('1.23E300', str);
  580. fmt = new goog.i18n.NumberFormat('0.####E0');
  581. str = fmt.format(-3.141592653e-271);
  582. assertEquals('-3.1416E-271', str);
  583. fmt = new goog.i18n.NumberFormat('00.000E00');
  584. str = fmt.format(-3.141592653e-271);
  585. assertEquals('-31.416E-272', str);
  586. fmt = new goog.i18n.NumberFormat('##0.######E000');
  587. str = fmt.format(-3.141592653e-271);
  588. assertEquals('-314.159265E-273', str);
  589. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  590. str = fmt.format(-3.141592653e-271);
  591. assertEquals('[3.142E-271]', str);
  592. fmt = new goog.i18n.NumberFormat('0.####E0');
  593. str = fmt.format(0);
  594. assertEquals('0E0', str);
  595. fmt = new goog.i18n.NumberFormat('00.000E00');
  596. str = fmt.format(0);
  597. assertEquals('00.000E00', str);
  598. fmt = new goog.i18n.NumberFormat('##0.######E000');
  599. str = fmt.format(0);
  600. assertEquals('0E000', str);
  601. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  602. str = fmt.format(0);
  603. assertEquals('0E0', str);
  604. fmt = new goog.i18n.NumberFormat('0.####E0');
  605. str = fmt.format(-1);
  606. assertEquals('-1E0', str);
  607. fmt = new goog.i18n.NumberFormat('00.000E00');
  608. str = fmt.format(-1);
  609. assertEquals('-10.000E-01', str);
  610. fmt = new goog.i18n.NumberFormat('##0.######E000');
  611. str = fmt.format(-1);
  612. assertEquals('-1E000', str);
  613. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  614. str = fmt.format(-1);
  615. assertEquals('[1E0]', str);
  616. fmt = new goog.i18n.NumberFormat('0.####E0');
  617. str = fmt.format(1);
  618. assertEquals('1E0', str);
  619. fmt = new goog.i18n.NumberFormat('00.000E00');
  620. str = fmt.format(1);
  621. assertEquals('10.000E-01', str);
  622. fmt = new goog.i18n.NumberFormat('##0.######E000');
  623. str = fmt.format(1);
  624. assertEquals('1E000', str);
  625. fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
  626. str = fmt.format(1);
  627. assertEquals('1E0', str);
  628. fmt = new goog.i18n.NumberFormat('#E0');
  629. str = fmt.format(12345.0);
  630. assertEquals('1E4', str);
  631. fmt = new goog.i18n.NumberFormat('0E0');
  632. str = fmt.format(12345.0);
  633. assertEquals('1E4', str);
  634. fmt = new goog.i18n.NumberFormat('##0.###E0');
  635. str = fmt.format(12345.0);
  636. assertEquals('12.345E3', str);
  637. fmt = new goog.i18n.NumberFormat('##0.###E0');
  638. str = fmt.format(12345.00001);
  639. assertEquals('12.345E3', str);
  640. fmt = new goog.i18n.NumberFormat('##0.###E0');
  641. str = fmt.format(12345);
  642. assertEquals('12.345E3', str);
  643. fmt = new goog.i18n.NumberFormat('##0.####E0');
  644. str = fmt.format(789.12345e-9);
  645. // Firefox 3.6.3 Linux is known to fail here with a rounding error.
  646. // fmt.format will return '789.1234E-9'.
  647. expectedFailures.expectFailureFor(isFirefox363Linux());
  648. try {
  649. assertEquals('789.1235E-9', str);
  650. } catch (e) {
  651. expectedFailures.handleException(e);
  652. }
  653. fmt = new goog.i18n.NumberFormat('##0.####E0');
  654. str = fmt.format(780.e-9);
  655. assertEquals('780E-9', str);
  656. fmt = new goog.i18n.NumberFormat('.###E0');
  657. str = fmt.format(45678.0);
  658. assertEquals('.457E5', str);
  659. fmt = new goog.i18n.NumberFormat('.###E0');
  660. str = fmt.format(0);
  661. assertEquals('.0E0', str);
  662. fmt = new goog.i18n.NumberFormat('#E0');
  663. str = fmt.format(45678000);
  664. assertEquals('5E7', str);
  665. fmt = new goog.i18n.NumberFormat('##E0');
  666. str = fmt.format(45678000);
  667. assertEquals('46E6', str);
  668. fmt = new goog.i18n.NumberFormat('####E0');
  669. str = fmt.format(45678000);
  670. assertEquals('4568E4', str);
  671. fmt = new goog.i18n.NumberFormat('0E0');
  672. str = fmt.format(45678000);
  673. assertEquals('5E7', str);
  674. fmt = new goog.i18n.NumberFormat('00E0');
  675. str = fmt.format(45678000);
  676. assertEquals('46E6', str);
  677. fmt = new goog.i18n.NumberFormat('000E0');
  678. str = fmt.format(45678000);
  679. assertEquals('457E5', str);
  680. fmt = new goog.i18n.NumberFormat('###E0');
  681. str = fmt.format(0.0000123);
  682. assertEquals('12E-6', str);
  683. fmt = new goog.i18n.NumberFormat('###E0');
  684. str = fmt.format(0.000123);
  685. assertEquals('123E-6', str);
  686. fmt = new goog.i18n.NumberFormat('###E0');
  687. str = fmt.format(0.00123);
  688. assertEquals('1E-3', str);
  689. fmt = new goog.i18n.NumberFormat('###E0');
  690. str = fmt.format(0.0123);
  691. assertEquals('12E-3', str);
  692. fmt = new goog.i18n.NumberFormat('###E0');
  693. str = fmt.format(0.123);
  694. assertEquals('123E-3', str);
  695. fmt = new goog.i18n.NumberFormat('###E0');
  696. str = fmt.format(1.23);
  697. assertEquals('1E0', str);
  698. fmt = new goog.i18n.NumberFormat('###E0');
  699. str = fmt.format(12.3);
  700. assertEquals('12E0', str);
  701. fmt = new goog.i18n.NumberFormat('###E0');
  702. str = fmt.format(123.0);
  703. assertEquals('123E0', str);
  704. fmt = new goog.i18n.NumberFormat('###E0');
  705. str = fmt.format(1230.0);
  706. assertEquals('1E3', str);
  707. }
  708. function testPlusSignInExponentPart() {
  709. var fmt;
  710. fmt = new goog.i18n.NumberFormat('0E+0');
  711. str = fmt.format(45678000);
  712. assertEquals('5E+7', str);
  713. }
  714. function testGroupingParse2() {
  715. var value;
  716. var fmt;
  717. fmt = new goog.i18n.NumberFormat('#,###');
  718. value = fmt.parse('1,234,567,890');
  719. assertEquals(1234567890, value);
  720. fmt = new goog.i18n.NumberFormat('#,###');
  721. value = fmt.parse('12,3456,7890');
  722. assertEquals(1234567890, value);
  723. fmt = new goog.i18n.NumberFormat('#');
  724. value = fmt.parse('1234567890');
  725. assertEquals(1234567890, value);
  726. }
  727. function testApis() {
  728. var fmt;
  729. var str;
  730. fmt = new goog.i18n.NumberFormat('#,###');
  731. str = fmt.format(1234567890);
  732. assertEquals('1,234,567,890', str);
  733. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00');
  734. str = fmt.format(1234.56);
  735. assertEquals('$1,234.56', str);
  736. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)');
  737. str = fmt.format(-1234.56);
  738. assertEquals('($1,234.56)', str);
  739. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00', 'SEK');
  740. str = fmt.format(1234.56);
  741. assertEquals('kr1,234.56', str);
  742. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)', 'SEK');
  743. str = fmt.format(-1234.56);
  744. assertEquals('(kr1,234.56)', str);
  745. }
  746. function testLocaleSwitch() {
  747. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
  748. // When this test is performed in test cluster, 2 out of 60 machines have
  749. // problem getting the symbol. It is likely to be caused by size of uncompiled
  750. // symbol file. There will not be an issue after it is compiled.
  751. if (goog.i18n.NumberFormatSymbols.DECIMAL_SEP ==
  752. goog.i18n.NumberFormatSymbols_en.DECIMAL_SEP) {
  753. // fails to load French symbols, skip the test.
  754. return;
  755. }
  756. var fmt = new goog.i18n.NumberFormat('#,###');
  757. var str = fmt.format(1234567890);
  758. assertEquals('1\u00a0234\u00a0567\u00a0890', str);
  759. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00');
  760. str = fmt.format(1234.56);
  761. assertEquals('\u20AC1\u00a0234,56', str);
  762. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)');
  763. str = fmt.format(-1234.56);
  764. assertEquals('(\u20AC1\u00a0234,56)', str);
  765. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00', 'SEK');
  766. str = fmt.format(1234.56);
  767. assertEquals('kr1\u00a0234,56', str);
  768. fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)', 'SEK');
  769. str = fmt.format(-1234.56);
  770. assertEquals('(kr1\u00a0234,56)', str);
  771. }
  772. function testFrenchParse() {
  773. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
  774. // When this test is performed in test cluster, 2 out of 60 machines have
  775. // problem getting the symbol. It is likely to be caused by size of uncompiled
  776. // symbol file. There will not be an issue after it is compiled.
  777. if (goog.i18n.NumberFormatSymbols.DECIMAL_SEP ==
  778. goog.i18n.NumberFormatSymbols_en.DECIMAL_SEP) {
  779. // fails to load French symbols, skip the test.
  780. return;
  781. }
  782. var fmt = new goog.i18n.NumberFormat('0.0000');
  783. var value = fmt.parse('0,30');
  784. assertEquals(0.30, value);
  785. fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  786. value = fmt.parse('0,30\u00A0\u20AC');
  787. assertEquals(0.30, value);
  788. fmt = new goog.i18n.NumberFormat('#,##0.00');
  789. value = fmt.parse('123 456,99');
  790. assertEquals(123456.99, value);
  791. fmt = new goog.i18n.NumberFormat('#,##0.00');
  792. value = fmt.parse('123\u00a0456,99');
  793. assertEquals(123456.99, value);
  794. fmt = new goog.i18n.NumberFormat('#,##0.00');
  795. value = fmt.parse('8 123\u00a0456,99');
  796. assertEquals(8123456.99, value);
  797. }
  798. function testFailParseShouldThrow() {
  799. var fmt = new goog.i18n.NumberFormat('0.0000');
  800. var value = fmt.parse('x');
  801. assertNaN(value);
  802. fmt = new goog.i18n.NumberFormat('0.000x');
  803. value = fmt.parse('3y');
  804. assertNaN(value);
  805. fmt = new goog.i18n.NumberFormat('x0.000');
  806. value = fmt.parse('y3');
  807. assertNaN(value);
  808. }
  809. /**
  810. * @return {boolean} Whether we're on Linux Firefox 3.6.3.
  811. */
  812. function isFirefox363Linux() {
  813. return goog.userAgent.product.FIREFOX && goog.userAgent.LINUX &&
  814. goog.userAgent.product.isVersion('3.6.3') &&
  815. !goog.userAgent.product.isVersion('3.6.4');
  816. }
  817. function testEnforceAscii() {
  818. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_ar;
  819. goog.i18n.NumberFormatSymbols_u_nu_latn =
  820. goog.i18n.NumberFormatSymbols_ar_u_nu_latn;
  821. var fmt = new goog.i18n.NumberFormat('0.0000%');
  822. var str = fmt.format(123.45789179565757);
  823. assertEquals('١٢٣٤٥٫٧٨٩٢٪؜', str);
  824. goog.i18n.NumberFormat.setEnforceAsciiDigits(true);
  825. fmt = new goog.i18n.NumberFormat('0.0000%');
  826. str = fmt.format(123.45789179565757);
  827. assertEquals('12345.7892‎%‎', str);
  828. }
  829. function testFractionDigits() {
  830. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  831. fmt.setMinimumFractionDigits(4);
  832. fmt.setMaximumFractionDigits(6);
  833. assertEquals('0.1230', fmt.format(0.123));
  834. assertEquals('0.123456', fmt.format(0.123456));
  835. assertEquals('0.123457', fmt.format(0.12345678));
  836. }
  837. function testFractionDigitsSetOutOfOrder() {
  838. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  839. // First, setup basic min/max
  840. fmt.setMinimumFractionDigits(2);
  841. fmt.setMaximumFractionDigits(2);
  842. // Now change to a lower min & max, but change the max value first so that it
  843. // is temporarily less than the current "min" value. This makes sure that we
  844. // don't throw an error.
  845. fmt.setMaximumFractionDigits(1);
  846. fmt.setMinimumFractionDigits(1);
  847. assertEquals('2.3', fmt.format(2.34));
  848. }
  849. function testFractionDigitsInvalid() {
  850. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  851. fmt.setMinimumFractionDigits(2);
  852. fmt.setMaximumFractionDigits(1);
  853. try {
  854. fmt.format(0.123);
  855. fail('Should have thrown exception.');
  856. } catch (e) {
  857. }
  858. }
  859. function testFractionDigitsTooHigh() {
  860. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  861. fmt.setMaximumFractionDigits(308);
  862. var err = assertThrows(function() {
  863. fmt.setMaximumFractionDigits(309);
  864. });
  865. assertEquals('Unsupported maximum fraction digits: 309', err.message);
  866. }
  867. function testSignificantDigitsEqualToMax() {
  868. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  869. fmt.setMinimumFractionDigits(0);
  870. fmt.setMaximumFractionDigits(2);
  871. fmt.setSignificantDigits(2);
  872. assertEquals('123', fmt.format(123.4));
  873. assertEquals('12', fmt.format(12.34));
  874. assertEquals('1.2', fmt.format(1.234));
  875. assertEquals('0.12', fmt.format(0.1234));
  876. assertEquals('0.13', fmt.format(0.1284));
  877. }
  878. function testSignificantDigitsLessThanMax() {
  879. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  880. fmt.setMinimumFractionDigits(0);
  881. fmt.setMaximumFractionDigits(4);
  882. fmt.setSignificantDigits(1);
  883. assertEquals('123', fmt.format(123.4));
  884. assertEquals('12', fmt.format(12.34));
  885. assertEquals('1', fmt.format(1.234));
  886. assertEquals('0.1', fmt.format(0.1234));
  887. assertEquals('0.2', fmt.format(0.1834));
  888. }
  889. function testSignificantDigitsMoreThanMax() {
  890. // Max fractional digits should be absolute
  891. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  892. fmt.setMinimumFractionDigits(0);
  893. fmt.setMaximumFractionDigits(2);
  894. fmt.setSignificantDigits(3);
  895. assertEquals('123', fmt.format(123.4));
  896. assertEquals('12.3', fmt.format(12.34));
  897. assertEquals('1.23', fmt.format(1.234));
  898. assertEquals('0.12', fmt.format(0.1234));
  899. assertEquals('0.13', fmt.format(0.1284));
  900. }
  901. function testSimpleCompactFrench() {
  902. // Switch to French.
  903. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
  904. goog.i18n.CompactNumberFormatSymbols =
  905. goog.i18n.CompactNumberFormatSymbols_fr;
  906. var fmt =
  907. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  908. var str = fmt.format(123400000);
  909. assertEquals('123\u00A0M', str);
  910. }
  911. function testSimpleCompactGerman() {
  912. // Switch to German.
  913. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_de;
  914. goog.i18n.CompactNumberFormatSymbols =
  915. goog.i18n.CompactNumberFormatSymbols_de;
  916. var fmt =
  917. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  918. // The german short compact decimal has a simple '0' for 1000's, which is
  919. // supposed to be interpreted as 'leave the number as-is'.
  920. // (The number itself will still be formatted with the '.', but no rounding)
  921. var str = fmt.format(1234);
  922. assertEquals('1.234', str);
  923. }
  924. function testSimpleCompact1() {
  925. var fmt =
  926. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  927. var str = fmt.format(1234);
  928. assertEquals('1.2K', str);
  929. }
  930. function testSimpleCompact2() {
  931. var fmt =
  932. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  933. var str = fmt.format(12345);
  934. assertEquals('12K', str);
  935. }
  936. function testRoundingCompact() {
  937. var fmt =
  938. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  939. var str = fmt.format(999999);
  940. assertEquals('1M', str); // as opposed to 1000k
  941. }
  942. function testRoundingCompactNegative() {
  943. var fmt =
  944. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  945. var str = fmt.format(-999999);
  946. assertEquals('-1M', str);
  947. }
  948. function testCompactSmall() {
  949. var fmt =
  950. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  951. var str = fmt.format(0.1234);
  952. assertEquals('0.12', str);
  953. }
  954. function testCompactLong() {
  955. var fmt =
  956. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_LONG);
  957. var str = fmt.format(12345);
  958. assertEquals('12 thousand', str);
  959. }
  960. function testCompactWithoutSignificant() {
  961. var fmt =
  962. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  963. fmt.setSignificantDigits(0);
  964. fmt.setMinimumFractionDigits(2);
  965. fmt.setMaximumFractionDigits(2);
  966. assertEquals('1.23K', fmt.format(1234));
  967. assertEquals('1.00K', fmt.format(1000));
  968. assertEquals('123.46K', fmt.format(123456.7));
  969. assertEquals('999.99K', fmt.format(999994));
  970. assertEquals('1.00M', fmt.format(999995));
  971. }
  972. function testCompactWithoutSignificant2() {
  973. var fmt =
  974. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  975. fmt.setSignificantDigits(0);
  976. fmt.setMinimumFractionDigits(0);
  977. fmt.setMaximumFractionDigits(2);
  978. assertEquals('1.23K', fmt.format(1234));
  979. assertEquals('1K', fmt.format(1000));
  980. assertEquals('123.46K', fmt.format(123456.7));
  981. assertEquals('999.99K', fmt.format(999994));
  982. assertEquals('1M', fmt.format(999995));
  983. }
  984. function testCompactFallbacks() {
  985. var cdfSymbols = {COMPACT_DECIMAL_SHORT_PATTERN: {'1000': {'other': '0K'}}};
  986. goog.i18n.CompactNumberFormatSymbols = cdfSymbols;
  987. var fmt =
  988. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_LONG);
  989. var str = fmt.format(220000000000000);
  990. assertEquals('220,000,000,000K', str);
  991. }
  992. function testShowTrailingZerosWithSignificantDigits() {
  993. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  994. fmt.setSignificantDigits(2);
  995. fmt.setShowTrailingZeros(true);
  996. assertEquals('2.0', fmt.format(2));
  997. assertEquals('2,000', fmt.format(2000));
  998. assertEquals('0.20', fmt.format(0.2));
  999. assertEquals('0.02', fmt.format(0.02));
  1000. assertEquals('0.002', fmt.format(0.002));
  1001. assertEquals('0.00', fmt.format(0));
  1002. fmt.setShowTrailingZeros(false);
  1003. assertEquals('2', fmt.format(2));
  1004. assertEquals('0.2', fmt.format(0.2));
  1005. }
  1006. function testShowTrailingZerosWithSignificantDigitsCompactShort() {
  1007. var fmt =
  1008. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  1009. fmt.setSignificantDigits(2);
  1010. fmt.setShowTrailingZeros(true);
  1011. assertEquals('2.0', fmt.format(2));
  1012. assertEquals('2.0K', fmt.format(2000));
  1013. assertEquals('20', fmt.format(20));
  1014. }
  1015. function testCurrencyCodeOrder() {
  1016. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
  1017. goog.i18n.CompactNumberFormatSymbols =
  1018. goog.i18n.CompactNumberFormatSymbols_fr;
  1019. var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  1020. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1021. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
  1022. goog.i18n.CompactNumberFormatSymbols =
  1023. goog.i18n.CompactNumberFormatSymbols_en;
  1024. var fmt1 = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  1025. assertTrue(fmt1.isCurrencyCodeBeforeValue());
  1026. // Check that we really have different formatters with different patterns
  1027. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1028. // Using custom patterns instead of standard locale ones
  1029. fmt = new goog.i18n.NumberFormat('\u00A4 #0');
  1030. assertTrue(fmt.isCurrencyCodeBeforeValue());
  1031. fmt = new goog.i18n.NumberFormat('\u00A4 0 and #');
  1032. assertTrue(fmt.isCurrencyCodeBeforeValue());
  1033. fmt = new goog.i18n.NumberFormat('#0 \u00A4');
  1034. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1035. fmt = new goog.i18n.NumberFormat('0 and # \u00A4');
  1036. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1037. fmt = new goog.i18n.NumberFormat('\u00A4 0');
  1038. assertTrue(fmt.isCurrencyCodeBeforeValue());
  1039. fmt = new goog.i18n.NumberFormat('0 \u00A4');
  1040. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1041. fmt = new goog.i18n.NumberFormat('\u00A4 #');
  1042. assertTrue(fmt.isCurrencyCodeBeforeValue());
  1043. fmt = new goog.i18n.NumberFormat('# \u00A4');
  1044. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1045. // Edge cases, should never happen (like #0 separated by currency symbol,
  1046. // or missing currency symbol, or missing both # and 0, or missing all)
  1047. // We still make sure we get reasonable results (as much as possible)
  1048. fmt = new goog.i18n.NumberFormat('0 \u00A4 #');
  1049. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1050. fmt = new goog.i18n.NumberFormat('# \u00A4 0');
  1051. assertFalse(fmt.isCurrencyCodeBeforeValue());
  1052. fmt = new goog.i18n.NumberFormat('\u00A4');
  1053. assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
  1054. fmt = new goog.i18n.NumberFormat('0');
  1055. assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
  1056. fmt = new goog.i18n.NumberFormat('#');
  1057. assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
  1058. fmt = new goog.i18n.NumberFormat('#0');
  1059. assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
  1060. fmt = new goog.i18n.NumberFormat('0 and #');
  1061. assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
  1062. fmt = new goog.i18n.NumberFormat('nothing');
  1063. assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
  1064. }
  1065. function testCompactWithBaseFormattingNumber() {
  1066. var fmt =
  1067. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  1068. fmt.setBaseFormatting(1000);
  1069. assertEquals('0.8K', fmt.format(800, 1000));
  1070. fmt.setBaseFormatting(null);
  1071. assertEquals('800', fmt.format(800, 1000));
  1072. fmt.setBaseFormatting(1000);
  1073. assertEquals('1,200K', fmt.format(1200000, 1000));
  1074. assertEquals('0.01K', fmt.format(10, 1000));
  1075. fmt.setSignificantDigits(0);
  1076. fmt.setMinimumFractionDigits(2);
  1077. assertEquals('0.00K', fmt.format(1, 1000));
  1078. }
  1079. function testCompactWithBaseFormattingFrench() {
  1080. // Switch to French.
  1081. stubs.set(goog.i18n, 'NumberFormatSymbols', goog.i18n.NumberFormatSymbols_fr);
  1082. stubs.set(
  1083. goog.i18n, 'CompactNumberFormatSymbols',
  1084. goog.i18n.CompactNumberFormatSymbols_fr);
  1085. var fmt =
  1086. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  1087. assertEquals('123\u00A0M', fmt.format(123400000));
  1088. fmt.setBaseFormatting(1000);
  1089. assertEquals('123\u00A0400\u00A0k', fmt.format(123400000));
  1090. }
  1091. function testGetBaseFormattingNumber() {
  1092. var fmt =
  1093. new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
  1094. assertEquals(null, fmt.getBaseFormatting());
  1095. fmt.setBaseFormatting(10000);
  1096. assertEquals(10000, fmt.getBaseFormatting());
  1097. }
  1098. // Moved Polish, Romanian, other currencies to tier 2, check that it works now
  1099. function testPolish() {
  1100. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_pl;
  1101. var fmPl = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  1102. assertEquals('100,00\u00A0z\u0142', fmPl.format(100)); // 100.00 zł
  1103. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_ro;
  1104. var fmRo = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
  1105. assertEquals('100,00\u00A0RON', fmRo.format(100));
  1106. goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
  1107. }
  1108. function testVerySmallNumberScientific() { // See b/30990076.
  1109. var f = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.SCIENTIFIC);
  1110. var result = f.format(5e-324);
  1111. assertEquals('5E-324', result);
  1112. }
  1113. function testVerySmallNumberDecimal() {
  1114. var f = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
  1115. f.setSignificantDigits(3);
  1116. f.setMaximumFractionDigits(100);
  1117. var expected = '0.' + goog.string.repeat('0', 89) + '387';
  1118. assertEquals(expected, f.format(3.87e-90));
  1119. expected = '0.' + goog.string.repeat('0', 8) + '387';
  1120. assertEquals(expected, f.format(3.87e-9));
  1121. expected = '0.' + goog.string.repeat('0', 89) + '342';
  1122. assertEquals(expected, f.format(3.42e-90));
  1123. expected = '0.' + goog.string.repeat('0', 8) + '342';
  1124. assertEquals(expected, f.format(3.42e-9));
  1125. f.setSignificantDigits(2);
  1126. expected = '0.' + goog.string.repeat('0', 89) + '39';
  1127. assertEquals(expected, f.format(3.87e-90));
  1128. expected = '0.' + goog.string.repeat('0', 8) + '39';
  1129. assertEquals(expected, f.format(3.87e-9));
  1130. expected = '0.' + goog.string.repeat('0', 89) + '34';
  1131. assertEquals(expected, f.format(3.42e-90));
  1132. expected = '0.' + goog.string.repeat('0', 8) + '34';
  1133. assertEquals(expected, f.format(3.42e-9));
  1134. f.setSignificantDigits(1);
  1135. expected = '0.' + goog.string.repeat('0', 89) + '4';
  1136. assertEquals(expected, f.format(3.87e-90));
  1137. expected = '0.' + goog.string.repeat('0', 8) + '4';
  1138. assertEquals(expected, f.format(3.87e-9));
  1139. expected = '0.' + goog.string.repeat('0', 89) + '3';
  1140. assertEquals(expected, f.format(3.42e-90));
  1141. expected = '0.' + goog.string.repeat('0', 8) + '3';
  1142. assertEquals(expected, f.format(3.42e-9));
  1143. }