12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343 |
- // Copyright 2006 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- goog.provide('goog.i18n.NumberFormatTest');
- goog.setTestOnly('goog.i18n.NumberFormatTest');
- goog.require('goog.i18n.CompactNumberFormatSymbols');
- goog.require('goog.i18n.CompactNumberFormatSymbols_de');
- goog.require('goog.i18n.CompactNumberFormatSymbols_en');
- goog.require('goog.i18n.CompactNumberFormatSymbols_fr');
- goog.require('goog.i18n.NumberFormat');
- goog.require('goog.i18n.NumberFormatSymbols');
- goog.require('goog.i18n.NumberFormatSymbols_ar');
- goog.require('goog.i18n.NumberFormatSymbols_ar_u_nu_latn');
- goog.require('goog.i18n.NumberFormatSymbols_de');
- goog.require('goog.i18n.NumberFormatSymbols_en');
- goog.require('goog.i18n.NumberFormatSymbols_fr');
- goog.require('goog.i18n.NumberFormatSymbols_pl');
- goog.require('goog.i18n.NumberFormatSymbols_ro');
- goog.require('goog.i18n.NumberFormatSymbols_u_nu_latn');
- goog.require('goog.string');
- goog.require('goog.testing.ExpectedFailures');
- goog.require('goog.testing.PropertyReplacer');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- goog.require('goog.userAgent.product');
- goog.require('goog.userAgent.product.isVersion');
- var expectedFailures;
- var stubs = new goog.testing.PropertyReplacer();
- function setUpPage() {
- expectedFailures = new goog.testing.ExpectedFailures();
- }
- function setUp() {
- // Always switch back to English on startup.
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
- goog.i18n.NumberFormatSymbols_u_nu_latn = goog.i18n.NumberFormatSymbols_en;
- goog.i18n.CompactNumberFormatSymbols =
- goog.i18n.CompactNumberFormatSymbols_en;
- goog.i18n.NumberFormat.setEnforceAsciiDigits(false);
- }
- function tearDown() {
- expectedFailures.handleTearDown();
- stubs.reset();
- }
- function veryBigNumberCompare(str1, str2) {
- return str1.length == str2.length &&
- str1.substring(0, 8) == str2.substring(0, 8);
- }
- function testVeryBigNumber() {
- var str;
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- str = fmt.format(1785599999999999888888888888888);
- // when comparing big number, various platform have small different in
- // precision. We have to tolerate that using veryBigNumberCompare.
- assertTrue(
- veryBigNumberCompare(
- '$1,785,599,999,999,999,400,000,000,000,000.00', str));
- str = fmt.format(1.7856E30);
- assertTrue(
- veryBigNumberCompare(
- '$1,785,599,999,999,999,400,000,000,000,000.00', str));
- str = fmt.format(1.3456E20);
- assertTrue(veryBigNumberCompare('$134,560,000,000,000,000,000.00', str));
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- str = fmt.format(1.3456E20);
- assertTrue(veryBigNumberCompare('134,559,999,999,999,980,000', str));
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
- str = fmt.format(1.3456E20);
- assertTrue(veryBigNumberCompare('13,456,000,000,000,000,000,000%', str));
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.SCIENTIFIC);
- str = fmt.format(1.3456E20);
- assertEquals('1E20', str);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- str = fmt.format(-1.234567890123456e306);
- assertEquals(1 + 1 + 306 + 306 / 3, str.length);
- assertEquals('-1,234,567,890,123,45', str.substr(0, 21));
- str = fmt.format(Infinity);
- assertEquals('\u221e', str);
- str = fmt.format(-Infinity);
- assertEquals('-\u221e', str);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- str = fmt.format(Infinity);
- assertEquals('\u221eT', str);
- str = fmt.format(-Infinity);
- assertEquals('-\u221eT', str);
- }
- function testStandardFormat() {
- var str;
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- str = fmt.format(1234.579);
- assertEquals('$1,234.58', str);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- str = fmt.format(1234.579);
- assertEquals('1,234.579', str);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
- str = fmt.format(1234.579);
- assertEquals('123,458%', str);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.SCIENTIFIC);
- str = fmt.format(1234.579);
- assertEquals('1E3', str);
- // Math.log(1000000)/Math.LN10 is strictly less than 6. Make sure it gets
- // formatted correctly.
- str = fmt.format(1000000);
- assertEquals('1E6', str);
- }
- function testNegativePercentage() {
- var str;
- var fmt = new goog.i18n.NumberFormat('#,##0.00%');
- str = fmt.format(-1234.56);
- assertEquals('-123,456.00%', str);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
- str = fmt.format(-1234.579);
- assertEquals('-123,458%', str);
- }
- function testNegativePercentagePattern() {
- var str;
- var fmt = new goog.i18n.NumberFormat('#,##0.00%;(#,##0.00%)');
- str = fmt.format(1234.56);
- assertEquals('123,456.00%', str);
- str = fmt.format(-1234.56);
- assertEquals('(123,456.00%)', str);
- }
- function testCustomPercentage() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.PERCENT);
- fmt.setMaximumFractionDigits(1);
- fmt.setMinimumFractionDigits(1);
- var str = fmt.format(0.1291);
- assertEquals('12.9%', str);
- fmt.setMaximumFractionDigits(2);
- fmt.setMinimumFractionDigits(1);
- str = fmt.format(0.129);
- assertEquals('12.9%', str);
- fmt.setMaximumFractionDigits(2);
- fmt.setMinimumFractionDigits(1);
- str = fmt.format(0.12);
- assertEquals('12.0%', str);
- fmt.setMaximumFractionDigits(2);
- fmt.setMinimumFractionDigits(1);
- str = fmt.format(0.12911);
- assertEquals('12.91%', str);
- }
- function testBasicParse() {
- var value;
- var fmt = new goog.i18n.NumberFormat('0.0000');
- value = fmt.parse('123.4579');
- assertEquals(123.4579, value);
- value = fmt.parse('+123.4579');
- assertEquals(123.4579, value);
- value = fmt.parse('-123.4579');
- assertEquals(-123.4579, value);
- }
- function testPrefixParse() {
- var value;
- var fmt = new goog.i18n.NumberFormat('0.0;(0.0)');
- value = fmt.parse('123.4579');
- assertEquals(123.4579, value);
- value = fmt.parse('(123.4579)');
- assertEquals(-123.4579, value);
- }
- function testPrecentParse() {
- var value;
- var fmt = new goog.i18n.NumberFormat('0.0;(0.0)');
- value = fmt.parse('123.4579%');
- assertEquals((123.4579 / 100), value);
- value = fmt.parse('(%123.4579)');
- assertEquals((-123.4579 / 100), value);
- value = fmt.parse('123.4579\u2030');
- assertEquals((123.4579 / 1000), value);
- value = fmt.parse('(\u2030123.4579)');
- assertEquals((-123.4579 / 1000), value);
- }
- function testPercentAndPerMillAdvance() {
- var value;
- var pos = [0];
- var fmt = new goog.i18n.NumberFormat('0');
- value = fmt.parse('120%', pos);
- assertEquals(1.2, value);
- assertEquals(4, pos[0]);
- pos[0] = 0;
- value = fmt.parse('120\u2030', pos);
- assertEquals(0.12, value);
- assertEquals(4, pos[0]);
- }
- function testPercentAndPerMillParsing() {
- var implicitFmt = new goog.i18n.NumberFormat('0;(0)');
- assertEquals(123 / 100, implicitFmt.parse("123%"));
- assertEquals(-123 / 100, implicitFmt.parse("(123%)"));
- assertEquals(123 / 1000, implicitFmt.parse("123‰"));
- assertEquals(-123 / 1000, implicitFmt.parse("(123‰)"));
- var explicitFmtPercent = new goog.i18n.NumberFormat('0%;(0%)');
- assertEquals(123 / 100, explicitFmtPercent.parse("123%"));
- assertEquals(-123 / 100, explicitFmtPercent.parse("(123%)"));
- var explicitFmtPermill = new goog.i18n.NumberFormat('0‰;(0‰)');
- assertEquals(123 / 1000, explicitFmtPermill.parse("123‰"));
- assertEquals(-123 / 1000, explicitFmtPermill.parse("(123‰)"));
- }
- function testInfinityParse() {
- var value;
- var fmt = new goog.i18n.NumberFormat('0.0;(0.0)');
- // gwt need to add those symbols first
- value = fmt.parse('\u221e');
- assertEquals(Number.POSITIVE_INFINITY, value);
- value = fmt.parse('(\u221e)');
- assertEquals(Number.NEGATIVE_INFINITY, value);
- }
- function testExponentParse() {
- var value;
- var fmt;
- fmt = new goog.i18n.NumberFormat('#E0');
- value = fmt.parse('1.234E3');
- assertEquals(1.234E+3, value);
- fmt = new goog.i18n.NumberFormat('0.###E0');
- value = fmt.parse('1.234E3');
- assertEquals(1.234E+3, value);
- fmt = new goog.i18n.NumberFormat('#E0');
- value = fmt.parse('1.2345E4');
- assertEquals(12345.0, value);
- value = fmt.parse('1.2345E4');
- assertEquals(12345.0, value);
- value = fmt.parse('1.2345E+4');
- assertEquals(12345.0, value);
- }
- function testGroupingParse() {
- var value;
- var fmt = new goog.i18n.NumberFormat('#,###');
- value = fmt.parse('1,234,567,890');
- assertEquals(1234567890, value);
- value = fmt.parse('12,3456,7890');
- assertEquals(1234567890, value);
- fmt = new goog.i18n.NumberFormat('#');
- value = fmt.parse('1234567890');
- assertEquals(1234567890, value);
- }
- function testParsingStop() {
- var pos = [0];
- var fmt = new goog.i18n.NumberFormat('###0.###E0');
- assertEquals(123.457, fmt.parse('123.457', pos));
- assertEquals(7, pos[0]);
- pos[0] = 0;
- assertEquals(123.457, fmt.parse('+123.457', pos));
- assertEquals(8, pos[0]);
- pos[0] = 0;
- assertEquals(123, fmt.parse('123 cars in the parking lot.', pos));
- assertEquals(3, pos[0]);
- pos[0] = 0;
- assertEquals(12, fmt.parse('12 + 12', pos));
- assertEquals(2, pos[0]);
- pos[0] = 0;
- assertEquals(12, fmt.parse('12+12', pos));
- assertEquals(2, pos[0]);
- pos[0] = 0;
- assertEquals(120, fmt.parse('1.2E+2', pos));
- assertEquals(6, pos[0]);
- pos[0] = 0;
- assertEquals(120, fmt.parse('1.2E+2-12', pos));
- assertEquals(6, pos[0]);
- }
- function testBasicFormat() {
- var fmt = new goog.i18n.NumberFormat('0.0000');
- var str = fmt.format(123.45789179565757);
- assertEquals('123.4579', str);
- }
- function testGrouping() {
- var str;
- var fmt = new goog.i18n.NumberFormat('#,###');
- str = fmt.format(1234567890);
- assertEquals('1,234,567,890', str);
- fmt = new goog.i18n.NumberFormat('#,####');
- str = fmt.format(1234567890);
- assertEquals('12,3456,7890', str);
- fmt = new goog.i18n.NumberFormat('#');
- str = fmt.format(1234567890);
- assertEquals('1234567890', str);
- }
- function testIndiaNumberGrouping() {
- // Test for a known grouping used and recognized in India
- var fmt = new goog.i18n.NumberFormat('#,##,###');
- var str = fmt.format(1);
- assertEquals('1', str);
- str = fmt.format(12);
- assertEquals('12', str);
- str = fmt.format(123);
- assertEquals('123', str);
- str = fmt.format(1234);
- assertEquals('1,234', str);
- str = fmt.format(12345);
- assertEquals('12,345', str);
- str = fmt.format(123456);
- assertEquals('1,23,456', str);
- str = fmt.format(1234567);
- assertEquals('12,34,567', str);
- str = fmt.format(12345678);
- assertEquals('1,23,45,678', str);
- str = fmt.format(123456789);
- assertEquals('12,34,56,789', str);
- str = fmt.format(1234567890);
- assertEquals('1,23,45,67,890', str);
- str = fmt.format(0);
- assertEquals('0', str);
- str = fmt.format(-1);
- assertEquals('-1', str);
- str = fmt.format(-12);
- assertEquals('-12', str);
- str = fmt.format(-123);
- assertEquals('-123', str);
- str = fmt.format(-1234);
- assertEquals('-1,234', str);
- str = fmt.format(-12345);
- assertEquals('-12,345', str);
- str = fmt.format(-123456);
- assertEquals('-1,23,456', str);
- str = fmt.format(-1234567);
- assertEquals('-12,34,567', str);
- str = fmt.format(-12345678);
- assertEquals('-1,23,45,678', str);
- str = fmt.format(-123456789);
- assertEquals('-12,34,56,789', str);
- str = fmt.format(-1234567890);
- assertEquals('-1,23,45,67,890', str);
- }
- function testUnknownNumberGroupings() {
- // Test for any future unknown grouping format in addition to India
- var fmt = new goog.i18n.NumberFormat('#,####,##,###');
- var str = fmt.format(1);
- assertEquals('1', str);
- str = fmt.format(12);
- assertEquals('12', str);
- str = fmt.format(123);
- assertEquals('123', str);
- str = fmt.format(1234);
- assertEquals('1,234', str);
- str = fmt.format(12345);
- assertEquals('12,345', str);
- str = fmt.format(123456);
- assertEquals('1,23,456', str);
- str = fmt.format(1234567);
- assertEquals('12,34,567', str);
- str = fmt.format(12345678);
- assertEquals('123,45,678', str);
- str = fmt.format(123456789);
- assertEquals('1234,56,789', str);
- str = fmt.format(1234567890);
- assertEquals('1,2345,67,890', str);
- str = fmt.format(11234567890);
- assertEquals('11,2345,67,890', str);
- str = fmt.format(111234567890);
- assertEquals('111,2345,67,890', str);
- str = fmt.format(1111234567890);
- assertEquals('1111,2345,67,890', str);
- str = fmt.format(11111234567890);
- assertEquals('1,1111,2345,67,890', str);
- str = fmt.format(0);
- assertEquals('0', str);
- str = fmt.format(-1);
- assertEquals('-1', str);
- str = fmt.format(-12);
- assertEquals('-12', str);
- str = fmt.format(-123);
- assertEquals('-123', str);
- str = fmt.format(-1234);
- assertEquals('-1,234', str);
- str = fmt.format(-12345);
- assertEquals('-12,345', str);
- str = fmt.format(-123456);
- assertEquals('-1,23,456', str);
- str = fmt.format(-1234567);
- assertEquals('-12,34,567', str);
- str = fmt.format(-12345678);
- assertEquals('-123,45,678', str);
- str = fmt.format(-123456789);
- assertEquals('-1234,56,789', str);
- str = fmt.format(-1234567890);
- assertEquals('-1,2345,67,890', str);
- str = fmt.format(-11234567890);
- assertEquals('-11,2345,67,890', str);
- str = fmt.format(-111234567890);
- assertEquals('-111,2345,67,890', str);
- str = fmt.format(-1111234567890);
- assertEquals('-1111,2345,67,890', str);
- str = fmt.format(-11111234567890);
- assertEquals('-1,1111,2345,67,890', str);
- fmt = new goog.i18n.NumberFormat('#,#,##,###,#');
- str = fmt.format(1);
- assertEquals('1', str);
- str = fmt.format(12);
- assertEquals('1,2', str);
- str = fmt.format(123);
- assertEquals('12,3', str);
- str = fmt.format(1234);
- assertEquals('123,4', str);
- str = fmt.format(12345);
- assertEquals('1,234,5', str);
- str = fmt.format(123456);
- assertEquals('12,345,6', str);
- str = fmt.format(1234567);
- assertEquals('1,23,456,7', str);
- str = fmt.format(12345678);
- assertEquals('1,2,34,567,8', str);
- str = fmt.format(123456789);
- assertEquals('1,2,3,45,678,9', str);
- str = fmt.format(1234567890);
- assertEquals('1,2,3,4,56,789,0', str);
- str = fmt.format(0);
- assertEquals('0', str);
- str = fmt.format(-1);
- assertEquals('-1', str);
- str = fmt.format(-12);
- assertEquals('-1,2', str);
- str = fmt.format(-123);
- assertEquals('-12,3', str);
- str = fmt.format(-1234);
- assertEquals('-123,4', str);
- str = fmt.format(-12345);
- assertEquals('-1,234,5', str);
- str = fmt.format(-123456);
- assertEquals('-12,345,6', str);
- str = fmt.format(-1234567);
- assertEquals('-1,23,456,7', str);
- str = fmt.format(-12345678);
- assertEquals('-1,2,34,567,8', str);
- str = fmt.format(-123456789);
- assertEquals('-1,2,3,45,678,9', str);
- str = fmt.format(-1234567890);
- assertEquals('-1,2,3,4,56,789,0', str);
- }
- function testPerMill() {
- var str;
- var fmt = new goog.i18n.NumberFormat('###.###\u2030');
- str = fmt.format(0.4857);
- assertEquals('485.7\u2030', str);
- }
- function testCurrency() {
- var str;
- var fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00');
- str = fmt.format(1234.56);
- assertEquals('$1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('-$1,234.56', str);
- fmt = new goog.i18n.NumberFormat(
- '\u00a4#,##0.00;-\u00a4#,##0.00', 'USD',
- goog.i18n.NumberFormat.CurrencyStyle.LOCAL);
- str = fmt.format(1234.56);
- assertEquals('$1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('-$1,234.56', str);
- fmt = new goog.i18n.NumberFormat(
- '\u00a4#,##0.00;-\u00a4#,##0.00', 'USD',
- goog.i18n.NumberFormat.CurrencyStyle.PORTABLE);
- str = fmt.format(1234.56);
- assertEquals('US$1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('-US$1,234.56', str);
- fmt = new goog.i18n.NumberFormat(
- '\u00a4#,##0.00;-\u00a4#,##0.00', 'USD',
- goog.i18n.NumberFormat.CurrencyStyle.GLOBAL);
- str = fmt.format(1234.56);
- assertEquals('USD $1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('-USD $1,234.56', str);
- fmt = new goog.i18n.NumberFormat(
- '\u00a4\u00a4 #,##0.00;-\u00a4\u00a4 #,##0.00');
- str = fmt.format(1234.56);
- assertEquals('USD 1,234.56', str);
- fmt = new goog.i18n.NumberFormat(
- '\u00a4\u00a4 #,##0.00;\u00a4\u00a4 -#,##0.00');
- str = fmt.format(-1234.56);
- assertEquals('USD -1,234.56', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00', 'BRL');
- str = fmt.format(1234.56);
- assertEquals('R$1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('-R$1,234.56', str);
- fmt = new goog.i18n.NumberFormat(
- '\u00a4\u00a4 #,##0.00;(\u00a4\u00a4 #,##0.00)', 'BRL');
- str = fmt.format(1234.56);
- assertEquals('BRL 1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('(BRL 1,234.56)', str);
- // Test implicit negative pattern.
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00');
- str = fmt.format(1234.56);
- assertEquals('$1,234.56', str);
- str = fmt.format(-1234.56);
- assertEquals('-$1,234.56', str);
- }
- function testQuotes() {
- var str;
- var fmt = new goog.i18n.NumberFormat('a\'fo\'\'o\'b#');
- str = fmt.format(123);
- assertEquals('afo\'ob123', str);
- fmt = new goog.i18n.NumberFormat('a\'\'b#');
- str = fmt.format(123);
- assertEquals('a\'b123', str);
- fmt = new goog.i18n.NumberFormat('a\'fo\'\'o\'b#');
- str = fmt.format(-123);
- assertEquals('-afo\'ob123', str);
- fmt = new goog.i18n.NumberFormat('a\'\'b#');
- str = fmt.format(-123);
- assertEquals('-a\'b123', str);
- }
- function testZeros() {
- var str;
- var fmt;
- fmt = new goog.i18n.NumberFormat('#.#');
- str = fmt.format(0);
- assertEquals('0', str);
- fmt = new goog.i18n.NumberFormat('#.');
- str = fmt.format(0);
- assertEquals('0.', str);
- fmt = new goog.i18n.NumberFormat('.#');
- str = fmt.format(0);
- assertEquals('.0', str);
- fmt = new goog.i18n.NumberFormat('#');
- str = fmt.format(0);
- assertEquals('0', str);
- fmt = new goog.i18n.NumberFormat('#0.#');
- str = fmt.format(0);
- assertEquals('0', str);
- fmt = new goog.i18n.NumberFormat('#0.');
- str = fmt.format(0);
- assertEquals('0.', str);
- fmt = new goog.i18n.NumberFormat('#.0');
- str = fmt.format(0);
- assertEquals('.0', str);
- fmt = new goog.i18n.NumberFormat('#');
- str = fmt.format(0);
- assertEquals('0', str);
- fmt = new goog.i18n.NumberFormat('000');
- str = fmt.format(0);
- assertEquals('000', str);
- }
- function testExponential() {
- var str;
- var fmt;
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(0.01234);
- assertEquals('1.234E-2', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(0.01234);
- assertEquals('12.340E-03', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(0.01234);
- assertEquals('12.34E-003', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(0.01234);
- assertEquals('1.234E-2', str);
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(123456789);
- assertEquals('1.2346E8', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(123456789);
- assertEquals('12.346E07', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(123456789);
- assertEquals('123.456789E006', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(123456789);
- assertEquals('1.235E8', str);
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(1.23e300);
- assertEquals('1.23E300', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(1.23e300);
- assertEquals('12.300E299', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(1.23e300);
- assertEquals('1.23E300', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(1.23e300);
- assertEquals('1.23E300', str);
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(-3.141592653e-271);
- assertEquals('-3.1416E-271', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(-3.141592653e-271);
- assertEquals('-31.416E-272', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(-3.141592653e-271);
- assertEquals('-314.159265E-273', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(-3.141592653e-271);
- assertEquals('[3.142E-271]', str);
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(0);
- assertEquals('0E0', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(0);
- assertEquals('00.000E00', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(0);
- assertEquals('0E000', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(0);
- assertEquals('0E0', str);
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(-1);
- assertEquals('-1E0', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(-1);
- assertEquals('-10.000E-01', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(-1);
- assertEquals('-1E000', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(-1);
- assertEquals('[1E0]', str);
- fmt = new goog.i18n.NumberFormat('0.####E0');
- str = fmt.format(1);
- assertEquals('1E0', str);
- fmt = new goog.i18n.NumberFormat('00.000E00');
- str = fmt.format(1);
- assertEquals('10.000E-01', str);
- fmt = new goog.i18n.NumberFormat('##0.######E000');
- str = fmt.format(1);
- assertEquals('1E000', str);
- fmt = new goog.i18n.NumberFormat('0.###E0;[0.###E0]');
- str = fmt.format(1);
- assertEquals('1E0', str);
- fmt = new goog.i18n.NumberFormat('#E0');
- str = fmt.format(12345.0);
- assertEquals('1E4', str);
- fmt = new goog.i18n.NumberFormat('0E0');
- str = fmt.format(12345.0);
- assertEquals('1E4', str);
- fmt = new goog.i18n.NumberFormat('##0.###E0');
- str = fmt.format(12345.0);
- assertEquals('12.345E3', str);
- fmt = new goog.i18n.NumberFormat('##0.###E0');
- str = fmt.format(12345.00001);
- assertEquals('12.345E3', str);
- fmt = new goog.i18n.NumberFormat('##0.###E0');
- str = fmt.format(12345);
- assertEquals('12.345E3', str);
- fmt = new goog.i18n.NumberFormat('##0.####E0');
- str = fmt.format(789.12345e-9);
- // Firefox 3.6.3 Linux is known to fail here with a rounding error.
- // fmt.format will return '789.1234E-9'.
- expectedFailures.expectFailureFor(isFirefox363Linux());
- try {
- assertEquals('789.1235E-9', str);
- } catch (e) {
- expectedFailures.handleException(e);
- }
- fmt = new goog.i18n.NumberFormat('##0.####E0');
- str = fmt.format(780.e-9);
- assertEquals('780E-9', str);
- fmt = new goog.i18n.NumberFormat('.###E0');
- str = fmt.format(45678.0);
- assertEquals('.457E5', str);
- fmt = new goog.i18n.NumberFormat('.###E0');
- str = fmt.format(0);
- assertEquals('.0E0', str);
- fmt = new goog.i18n.NumberFormat('#E0');
- str = fmt.format(45678000);
- assertEquals('5E7', str);
- fmt = new goog.i18n.NumberFormat('##E0');
- str = fmt.format(45678000);
- assertEquals('46E6', str);
- fmt = new goog.i18n.NumberFormat('####E0');
- str = fmt.format(45678000);
- assertEquals('4568E4', str);
- fmt = new goog.i18n.NumberFormat('0E0');
- str = fmt.format(45678000);
- assertEquals('5E7', str);
- fmt = new goog.i18n.NumberFormat('00E0');
- str = fmt.format(45678000);
- assertEquals('46E6', str);
- fmt = new goog.i18n.NumberFormat('000E0');
- str = fmt.format(45678000);
- assertEquals('457E5', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(0.0000123);
- assertEquals('12E-6', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(0.000123);
- assertEquals('123E-6', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(0.00123);
- assertEquals('1E-3', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(0.0123);
- assertEquals('12E-3', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(0.123);
- assertEquals('123E-3', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(1.23);
- assertEquals('1E0', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(12.3);
- assertEquals('12E0', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(123.0);
- assertEquals('123E0', str);
- fmt = new goog.i18n.NumberFormat('###E0');
- str = fmt.format(1230.0);
- assertEquals('1E3', str);
- }
- function testPlusSignInExponentPart() {
- var fmt;
- fmt = new goog.i18n.NumberFormat('0E+0');
- str = fmt.format(45678000);
- assertEquals('5E+7', str);
- }
- function testGroupingParse2() {
- var value;
- var fmt;
- fmt = new goog.i18n.NumberFormat('#,###');
- value = fmt.parse('1,234,567,890');
- assertEquals(1234567890, value);
- fmt = new goog.i18n.NumberFormat('#,###');
- value = fmt.parse('12,3456,7890');
- assertEquals(1234567890, value);
- fmt = new goog.i18n.NumberFormat('#');
- value = fmt.parse('1234567890');
- assertEquals(1234567890, value);
- }
- function testApis() {
- var fmt;
- var str;
- fmt = new goog.i18n.NumberFormat('#,###');
- str = fmt.format(1234567890);
- assertEquals('1,234,567,890', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00');
- str = fmt.format(1234.56);
- assertEquals('$1,234.56', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)');
- str = fmt.format(-1234.56);
- assertEquals('($1,234.56)', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00', 'SEK');
- str = fmt.format(1234.56);
- assertEquals('kr1,234.56', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)', 'SEK');
- str = fmt.format(-1234.56);
- assertEquals('(kr1,234.56)', str);
- }
- function testLocaleSwitch() {
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
- // When this test is performed in test cluster, 2 out of 60 machines have
- // problem getting the symbol. It is likely to be caused by size of uncompiled
- // symbol file. There will not be an issue after it is compiled.
- if (goog.i18n.NumberFormatSymbols.DECIMAL_SEP ==
- goog.i18n.NumberFormatSymbols_en.DECIMAL_SEP) {
- // fails to load French symbols, skip the test.
- return;
- }
- var fmt = new goog.i18n.NumberFormat('#,###');
- var str = fmt.format(1234567890);
- assertEquals('1\u00a0234\u00a0567\u00a0890', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00');
- str = fmt.format(1234.56);
- assertEquals('\u20AC1\u00a0234,56', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)');
- str = fmt.format(-1234.56);
- assertEquals('(\u20AC1\u00a0234,56)', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;-\u00a4#,##0.00', 'SEK');
- str = fmt.format(1234.56);
- assertEquals('kr1\u00a0234,56', str);
- fmt = new goog.i18n.NumberFormat('\u00a4#,##0.00;(\u00a4#,##0.00)', 'SEK');
- str = fmt.format(-1234.56);
- assertEquals('(kr1\u00a0234,56)', str);
- }
- function testFrenchParse() {
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
- // When this test is performed in test cluster, 2 out of 60 machines have
- // problem getting the symbol. It is likely to be caused by size of uncompiled
- // symbol file. There will not be an issue after it is compiled.
- if (goog.i18n.NumberFormatSymbols.DECIMAL_SEP ==
- goog.i18n.NumberFormatSymbols_en.DECIMAL_SEP) {
- // fails to load French symbols, skip the test.
- return;
- }
- var fmt = new goog.i18n.NumberFormat('0.0000');
- var value = fmt.parse('0,30');
- assertEquals(0.30, value);
- fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- value = fmt.parse('0,30\u00A0\u20AC');
- assertEquals(0.30, value);
- fmt = new goog.i18n.NumberFormat('#,##0.00');
- value = fmt.parse('123 456,99');
- assertEquals(123456.99, value);
- fmt = new goog.i18n.NumberFormat('#,##0.00');
- value = fmt.parse('123\u00a0456,99');
- assertEquals(123456.99, value);
- fmt = new goog.i18n.NumberFormat('#,##0.00');
- value = fmt.parse('8 123\u00a0456,99');
- assertEquals(8123456.99, value);
- }
- function testFailParseShouldThrow() {
- var fmt = new goog.i18n.NumberFormat('0.0000');
- var value = fmt.parse('x');
- assertNaN(value);
- fmt = new goog.i18n.NumberFormat('0.000x');
- value = fmt.parse('3y');
- assertNaN(value);
- fmt = new goog.i18n.NumberFormat('x0.000');
- value = fmt.parse('y3');
- assertNaN(value);
- }
- /**
- * @return {boolean} Whether we're on Linux Firefox 3.6.3.
- */
- function isFirefox363Linux() {
- return goog.userAgent.product.FIREFOX && goog.userAgent.LINUX &&
- goog.userAgent.product.isVersion('3.6.3') &&
- !goog.userAgent.product.isVersion('3.6.4');
- }
- function testEnforceAscii() {
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_ar;
- goog.i18n.NumberFormatSymbols_u_nu_latn =
- goog.i18n.NumberFormatSymbols_ar_u_nu_latn;
- var fmt = new goog.i18n.NumberFormat('0.0000%');
- var str = fmt.format(123.45789179565757);
- assertEquals('١٢٣٤٥٫٧٨٩٢٪', str);
- goog.i18n.NumberFormat.setEnforceAsciiDigits(true);
- fmt = new goog.i18n.NumberFormat('0.0000%');
- str = fmt.format(123.45789179565757);
- assertEquals('12345.7892%', str);
- }
- function testFractionDigits() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setMinimumFractionDigits(4);
- fmt.setMaximumFractionDigits(6);
- assertEquals('0.1230', fmt.format(0.123));
- assertEquals('0.123456', fmt.format(0.123456));
- assertEquals('0.123457', fmt.format(0.12345678));
- }
- function testFractionDigitsSetOutOfOrder() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- // First, setup basic min/max
- fmt.setMinimumFractionDigits(2);
- fmt.setMaximumFractionDigits(2);
- // Now change to a lower min & max, but change the max value first so that it
- // is temporarily less than the current "min" value. This makes sure that we
- // don't throw an error.
- fmt.setMaximumFractionDigits(1);
- fmt.setMinimumFractionDigits(1);
- assertEquals('2.3', fmt.format(2.34));
- }
- function testFractionDigitsInvalid() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setMinimumFractionDigits(2);
- fmt.setMaximumFractionDigits(1);
- try {
- fmt.format(0.123);
- fail('Should have thrown exception.');
- } catch (e) {
- }
- }
- function testFractionDigitsTooHigh() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setMaximumFractionDigits(308);
- var err = assertThrows(function() {
- fmt.setMaximumFractionDigits(309);
- });
- assertEquals('Unsupported maximum fraction digits: 309', err.message);
- }
- function testSignificantDigitsEqualToMax() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setMinimumFractionDigits(0);
- fmt.setMaximumFractionDigits(2);
- fmt.setSignificantDigits(2);
- assertEquals('123', fmt.format(123.4));
- assertEquals('12', fmt.format(12.34));
- assertEquals('1.2', fmt.format(1.234));
- assertEquals('0.12', fmt.format(0.1234));
- assertEquals('0.13', fmt.format(0.1284));
- }
- function testSignificantDigitsLessThanMax() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setMinimumFractionDigits(0);
- fmt.setMaximumFractionDigits(4);
- fmt.setSignificantDigits(1);
- assertEquals('123', fmt.format(123.4));
- assertEquals('12', fmt.format(12.34));
- assertEquals('1', fmt.format(1.234));
- assertEquals('0.1', fmt.format(0.1234));
- assertEquals('0.2', fmt.format(0.1834));
- }
- function testSignificantDigitsMoreThanMax() {
- // Max fractional digits should be absolute
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setMinimumFractionDigits(0);
- fmt.setMaximumFractionDigits(2);
- fmt.setSignificantDigits(3);
- assertEquals('123', fmt.format(123.4));
- assertEquals('12.3', fmt.format(12.34));
- assertEquals('1.23', fmt.format(1.234));
- assertEquals('0.12', fmt.format(0.1234));
- assertEquals('0.13', fmt.format(0.1284));
- }
- function testSimpleCompactFrench() {
- // Switch to French.
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
- goog.i18n.CompactNumberFormatSymbols =
- goog.i18n.CompactNumberFormatSymbols_fr;
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- var str = fmt.format(123400000);
- assertEquals('123\u00A0M', str);
- }
- function testSimpleCompactGerman() {
- // Switch to German.
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_de;
- goog.i18n.CompactNumberFormatSymbols =
- goog.i18n.CompactNumberFormatSymbols_de;
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- // The german short compact decimal has a simple '0' for 1000's, which is
- // supposed to be interpreted as 'leave the number as-is'.
- // (The number itself will still be formatted with the '.', but no rounding)
- var str = fmt.format(1234);
- assertEquals('1.234', str);
- }
- function testSimpleCompact1() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- var str = fmt.format(1234);
- assertEquals('1.2K', str);
- }
- function testSimpleCompact2() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- var str = fmt.format(12345);
- assertEquals('12K', str);
- }
- function testRoundingCompact() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- var str = fmt.format(999999);
- assertEquals('1M', str); // as opposed to 1000k
- }
- function testRoundingCompactNegative() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- var str = fmt.format(-999999);
- assertEquals('-1M', str);
- }
- function testCompactSmall() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- var str = fmt.format(0.1234);
- assertEquals('0.12', str);
- }
- function testCompactLong() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_LONG);
- var str = fmt.format(12345);
- assertEquals('12 thousand', str);
- }
- function testCompactWithoutSignificant() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- fmt.setSignificantDigits(0);
- fmt.setMinimumFractionDigits(2);
- fmt.setMaximumFractionDigits(2);
- assertEquals('1.23K', fmt.format(1234));
- assertEquals('1.00K', fmt.format(1000));
- assertEquals('123.46K', fmt.format(123456.7));
- assertEquals('999.99K', fmt.format(999994));
- assertEquals('1.00M', fmt.format(999995));
- }
- function testCompactWithoutSignificant2() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- fmt.setSignificantDigits(0);
- fmt.setMinimumFractionDigits(0);
- fmt.setMaximumFractionDigits(2);
- assertEquals('1.23K', fmt.format(1234));
- assertEquals('1K', fmt.format(1000));
- assertEquals('123.46K', fmt.format(123456.7));
- assertEquals('999.99K', fmt.format(999994));
- assertEquals('1M', fmt.format(999995));
- }
- function testCompactFallbacks() {
- var cdfSymbols = {COMPACT_DECIMAL_SHORT_PATTERN: {'1000': {'other': '0K'}}};
- goog.i18n.CompactNumberFormatSymbols = cdfSymbols;
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_LONG);
- var str = fmt.format(220000000000000);
- assertEquals('220,000,000,000K', str);
- }
- function testShowTrailingZerosWithSignificantDigits() {
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- fmt.setSignificantDigits(2);
- fmt.setShowTrailingZeros(true);
- assertEquals('2.0', fmt.format(2));
- assertEquals('2,000', fmt.format(2000));
- assertEquals('0.20', fmt.format(0.2));
- assertEquals('0.02', fmt.format(0.02));
- assertEquals('0.002', fmt.format(0.002));
- assertEquals('0.00', fmt.format(0));
- fmt.setShowTrailingZeros(false);
- assertEquals('2', fmt.format(2));
- assertEquals('0.2', fmt.format(0.2));
- }
- function testShowTrailingZerosWithSignificantDigitsCompactShort() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- fmt.setSignificantDigits(2);
- fmt.setShowTrailingZeros(true);
- assertEquals('2.0', fmt.format(2));
- assertEquals('2.0K', fmt.format(2000));
- assertEquals('20', fmt.format(20));
- }
- function testCurrencyCodeOrder() {
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_fr;
- goog.i18n.CompactNumberFormatSymbols =
- goog.i18n.CompactNumberFormatSymbols_fr;
- var fmt = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
- goog.i18n.CompactNumberFormatSymbols =
- goog.i18n.CompactNumberFormatSymbols_en;
- var fmt1 = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- assertTrue(fmt1.isCurrencyCodeBeforeValue());
- // Check that we really have different formatters with different patterns
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- // Using custom patterns instead of standard locale ones
- fmt = new goog.i18n.NumberFormat('\u00A4 #0');
- assertTrue(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('\u00A4 0 and #');
- assertTrue(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('#0 \u00A4');
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('0 and # \u00A4');
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('\u00A4 0');
- assertTrue(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('0 \u00A4');
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('\u00A4 #');
- assertTrue(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('# \u00A4');
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- // Edge cases, should never happen (like #0 separated by currency symbol,
- // or missing currency symbol, or missing both # and 0, or missing all)
- // We still make sure we get reasonable results (as much as possible)
- fmt = new goog.i18n.NumberFormat('0 \u00A4 #');
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('# \u00A4 0');
- assertFalse(fmt.isCurrencyCodeBeforeValue());
- fmt = new goog.i18n.NumberFormat('\u00A4');
- assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
- fmt = new goog.i18n.NumberFormat('0');
- assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
- fmt = new goog.i18n.NumberFormat('#');
- assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
- fmt = new goog.i18n.NumberFormat('#0');
- assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
- fmt = new goog.i18n.NumberFormat('0 and #');
- assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
- fmt = new goog.i18n.NumberFormat('nothing');
- assertTrue(fmt.isCurrencyCodeBeforeValue()); // currency first, en_US style
- }
- function testCompactWithBaseFormattingNumber() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- fmt.setBaseFormatting(1000);
- assertEquals('0.8K', fmt.format(800, 1000));
- fmt.setBaseFormatting(null);
- assertEquals('800', fmt.format(800, 1000));
- fmt.setBaseFormatting(1000);
- assertEquals('1,200K', fmt.format(1200000, 1000));
- assertEquals('0.01K', fmt.format(10, 1000));
- fmt.setSignificantDigits(0);
- fmt.setMinimumFractionDigits(2);
- assertEquals('0.00K', fmt.format(1, 1000));
- }
- function testCompactWithBaseFormattingFrench() {
- // Switch to French.
- stubs.set(goog.i18n, 'NumberFormatSymbols', goog.i18n.NumberFormatSymbols_fr);
- stubs.set(
- goog.i18n, 'CompactNumberFormatSymbols',
- goog.i18n.CompactNumberFormatSymbols_fr);
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- assertEquals('123\u00A0M', fmt.format(123400000));
- fmt.setBaseFormatting(1000);
- assertEquals('123\u00A0400\u00A0k', fmt.format(123400000));
- }
- function testGetBaseFormattingNumber() {
- var fmt =
- new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.COMPACT_SHORT);
- assertEquals(null, fmt.getBaseFormatting());
- fmt.setBaseFormatting(10000);
- assertEquals(10000, fmt.getBaseFormatting());
- }
- // Moved Polish, Romanian, other currencies to tier 2, check that it works now
- function testPolish() {
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_pl;
- var fmPl = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- assertEquals('100,00\u00A0z\u0142', fmPl.format(100)); // 100.00 zł
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_ro;
- var fmRo = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.CURRENCY);
- assertEquals('100,00\u00A0RON', fmRo.format(100));
- goog.i18n.NumberFormatSymbols = goog.i18n.NumberFormatSymbols_en;
- }
- function testVerySmallNumberScientific() { // See b/30990076.
- var f = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.SCIENTIFIC);
- var result = f.format(5e-324);
- assertEquals('5E-324', result);
- }
- function testVerySmallNumberDecimal() {
- var f = new goog.i18n.NumberFormat(goog.i18n.NumberFormat.Format.DECIMAL);
- f.setSignificantDigits(3);
- f.setMaximumFractionDigits(100);
- var expected = '0.' + goog.string.repeat('0', 89) + '387';
- assertEquals(expected, f.format(3.87e-90));
- expected = '0.' + goog.string.repeat('0', 8) + '387';
- assertEquals(expected, f.format(3.87e-9));
- expected = '0.' + goog.string.repeat('0', 89) + '342';
- assertEquals(expected, f.format(3.42e-90));
- expected = '0.' + goog.string.repeat('0', 8) + '342';
- assertEquals(expected, f.format(3.42e-9));
- f.setSignificantDigits(2);
- expected = '0.' + goog.string.repeat('0', 89) + '39';
- assertEquals(expected, f.format(3.87e-90));
- expected = '0.' + goog.string.repeat('0', 8) + '39';
- assertEquals(expected, f.format(3.87e-9));
- expected = '0.' + goog.string.repeat('0', 89) + '34';
- assertEquals(expected, f.format(3.42e-90));
- expected = '0.' + goog.string.repeat('0', 8) + '34';
- assertEquals(expected, f.format(3.42e-9));
- f.setSignificantDigits(1);
- expected = '0.' + goog.string.repeat('0', 89) + '4';
- assertEquals(expected, f.format(3.87e-90));
- expected = '0.' + goog.string.repeat('0', 8) + '4';
- assertEquals(expected, f.format(3.87e-9));
- expected = '0.' + goog.string.repeat('0', 89) + '3';
- assertEquals(expected, f.format(3.42e-90));
- expected = '0.' + goog.string.repeat('0', 8) + '3';
- assertEquals(expected, f.format(3.42e-9));
- }
|