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