| 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 |
/** |
| 16 |
* @fileoverview Number format/parse library with locale support. |
| 17 |
*/ |
| 18 |
|
| 19 |
|
| 20 |
/** |
| 21 |
* Namespace for locale number format functions |
| 22 |
*/ |
| 23 |
goog.provide('goog.i18n.NumberFormat'); |
| 24 |
goog.provide('goog.i18n.NumberFormat.CurrencyStyle'); |
| 25 |
goog.provide('goog.i18n.NumberFormat.Format'); |
| 26 |
|
| 27 |
goog.require('goog.asserts'); |
| 28 |
goog.require('goog.i18n.CompactNumberFormatSymbols'); |
| 29 |
goog.require('goog.i18n.NumberFormatSymbols'); |
| 30 |
goog.require('goog.i18n.NumberFormatSymbols_u_nu_latn'); |
| 31 |
goog.require('goog.i18n.currency'); |
| 32 |
goog.require('goog.math'); |
| 33 |
goog.require('goog.string'); |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* Constructor of NumberFormat. |
| 39 |
* @param {number|string} pattern The number that indicates a predefined |
| 40 |
* number format pattern. |
| 41 |
* @param {string=} opt_currency Optional international currency |
| 42 |
* code. This determines the currency code/symbol used in format/parse. If |
| 43 |
* not given, the currency code for current locale will be used. |
| 44 |
* @param {number=} opt_currencyStyle currency style, value defined in |
| 45 |
* goog.i18n.NumberFormat.CurrencyStyle. |
| 46 |
* @constructor |
| 47 |
*/ |
| 48 |
goog.i18n.NumberFormat = function(pattern, opt_currency, opt_currencyStyle) { |
| 49 |
/** @private {string|undefined} */ |
| 50 |
this.intlCurrencyCode_ = opt_currency; |
| 51 |
|
| 52 |
/** @private {number} */ |
| 53 |
this.currencyStyle_ = |
| 54 |
opt_currencyStyle || goog.i18n.NumberFormat.CurrencyStyle.LOCAL; |
| 55 |
|
| 56 |
/** @private {number} */ |
| 57 |
this.maximumIntegerDigits_ = 40; |
| 58 |
/** @private {number} */ |
| 59 |
this.minimumIntegerDigits_ = 1; |
| 60 |
/** @private {number} */ |
| 61 |
this.significantDigits_ = 0; // invariant, <= maximumFractionDigits |
| 62 |
/** @private {number} */ |
| 63 |
this.maximumFractionDigits_ = 3; // invariant, >= minFractionDigits |
| 64 |
/** @private {number} */ |
| 65 |
this.minimumFractionDigits_ = 0; |
| 66 |
/** @private {number} */ |
| 67 |
this.minExponentDigits_ = 0; |
| 68 |
/** @private {boolean} */ |
| 69 |
this.useSignForPositiveExponent_ = false; |
| 70 |
|
| 71 |
/** |
| 72 |
* Whether to show trailing zeros in the fraction when significantDigits_ is |
| 73 |
* positive. |
| 74 |
* @private {boolean} |
| 75 |
*/ |
| 76 |
this.showTrailingZeros_ = false; |
| 77 |
|
| 78 |
/** @private {string} */ |
| 79 |
this.positivePrefix_ = ''; |
| 80 |
/** @private {string} */ |
| 81 |
this.positiveSuffix_ = ''; |
| 82 |
/** @private {string} */ |
| 83 |
this.negativePrefix_ = |
| 84 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().MINUS_SIGN; |
| 85 |
/** @private {string} */ |
| 86 |
this.negativeSuffix_ = ''; |
| 87 |
|
| 88 |
// The multiplier for use in percent, per mille, etc. |
| 89 |
/** @private {number} */ |
| 90 |
this.multiplier_ = 1; |
| 91 |
|
| 92 |
/** |
| 93 |
* True if the percent/permill sign of the negative pattern is expected. |
| 94 |
* @private {!boolean} |
| 95 |
*/ |
| 96 |
this.negativePercentSignExpected_ = false; |
| 97 |
|
| 98 |
/** |
| 99 |
* The grouping array is used to store the values of each number group |
| 100 |
* following left of the decimal place. For example, a number group with |
| 101 |
* goog.i18n.NumberFormat('#,##,###') should have [3,2] where 2 is the |
| 102 |
* repeated number group following a fixed number grouping of size 3. |
| 103 |
* @private {!Array<number>} |
| 104 |
*/ |
| 105 |
this.groupingArray_ = []; |
| 106 |
|
| 107 |
/** @private {boolean} */ |
| 108 |
this.decimalSeparatorAlwaysShown_ = false; |
| 109 |
/** @private {boolean} */ |
| 110 |
this.useExponentialNotation_ = false; |
| 111 |
/** @private {goog.i18n.NumberFormat.CompactStyle} */ |
| 112 |
this.compactStyle_ = goog.i18n.NumberFormat.CompactStyle.NONE; |
| 113 |
|
| 114 |
/** |
| 115 |
* The number to base the formatting on when using compact styles, or null |
| 116 |
* if formatting should not be based on another number. |
| 117 |
* @type {?number} |
| 118 |
* @private |
| 119 |
*/ |
| 120 |
this.baseFormattingNumber_ = null; |
| 121 |
|
| 122 |
/** @private {string} */ |
| 123 |
this.pattern_; |
| 124 |
|
| 125 |
if (typeof pattern == 'number') { |
| 126 |
this.applyStandardPattern_(pattern); |
| 127 |
} else { |
| 128 |
this.applyPattern_(pattern); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
/** |
| 134 |
* Standard number formatting patterns. |
| 135 |
* @enum {number} |
| 136 |
*/ |
| 137 |
goog.i18n.NumberFormat.Format = { |
| 138 |
DECIMAL: 1, |
| 139 |
SCIENTIFIC: 2, |
| 140 |
PERCENT: 3, |
| 141 |
CURRENCY: 4, |
| 142 |
COMPACT_SHORT: 5, |
| 143 |
COMPACT_LONG: 6 |
| 144 |
}; |
| 145 |
|
| 146 |
|
| 147 |
/** |
| 148 |
* Currency styles. |
| 149 |
* @enum {number} |
| 150 |
*/ |
| 151 |
goog.i18n.NumberFormat.CurrencyStyle = { |
| 152 |
LOCAL: 0, // currency style as it is used in its circulating country. |
| 153 |
PORTABLE: 1, // currency style that differentiate it from other popular ones. |
| 154 |
GLOBAL: 2 // currency style that is unique among all currencies. |
| 155 |
}; |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* Compacting styles. |
| 160 |
* @enum {number} |
| 161 |
*/ |
| 162 |
goog.i18n.NumberFormat.CompactStyle = { |
| 163 |
NONE: 0, // Don't compact. |
| 164 |
SHORT: 1, // Short compact form, such as 1.2B. |
| 165 |
LONG: 2 // Long compact form, such as 1.2 billion. |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* If the usage of Ascii digits should be enforced. |
| 171 |
* @type {boolean} |
| 172 |
* @private |
| 173 |
*/ |
| 174 |
goog.i18n.NumberFormat.enforceAsciiDigits_ = false; |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* Set if the usage of Ascii digits in formatting should be enforced. |
| 179 |
* NOTE: This function must be called before constructing NumberFormat. |
| 180 |
* |
| 181 |
* @param {boolean} doEnforce Boolean value about if Ascii digits should be |
| 182 |
* enforced. |
| 183 |
*/ |
| 184 |
goog.i18n.NumberFormat.setEnforceAsciiDigits = function(doEnforce) { |
| 185 |
goog.i18n.NumberFormat.enforceAsciiDigits_ = doEnforce; |
| 186 |
}; |
| 187 |
|
| 188 |
|
| 189 |
/** |
| 190 |
* Return if Ascii digits is enforced. |
| 191 |
* @return {boolean} If Ascii digits is enforced. |
| 192 |
*/ |
| 193 |
goog.i18n.NumberFormat.isEnforceAsciiDigits = function() { |
| 194 |
return goog.i18n.NumberFormat.enforceAsciiDigits_; |
| 195 |
}; |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Returns the current NumberFormatSymbols. |
| 200 |
* @return {!Object} |
| 201 |
* @private |
| 202 |
*/ |
| 203 |
goog.i18n.NumberFormat.getNumberFormatSymbols_ = function() { |
| 204 |
return goog.i18n.NumberFormat.enforceAsciiDigits_ ? |
| 205 |
goog.i18n.NumberFormatSymbols_u_nu_latn : |
| 206 |
goog.i18n.NumberFormatSymbols; |
| 207 |
}; |
| 208 |
|
| 209 |
|
| 210 |
/** |
| 211 |
* Returns the currency code. |
| 212 |
* @return {string} |
| 213 |
* @private |
| 214 |
*/ |
| 215 |
goog.i18n.NumberFormat.prototype.getCurrencyCode_ = function() { |
| 216 |
return this.intlCurrencyCode_ || |
| 217 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().DEF_CURRENCY_CODE; |
| 218 |
}; |
| 219 |
|
| 220 |
|
| 221 |
/** |
| 222 |
* Sets minimum number of fraction digits. |
| 223 |
* @param {number} min the minimum. |
| 224 |
* @return {!goog.i18n.NumberFormat} Reference to this NumberFormat object. |
| 225 |
*/ |
| 226 |
goog.i18n.NumberFormat.prototype.setMinimumFractionDigits = function(min) { |
| 227 |
if (this.significantDigits_ > 0 && min > 0) { |
| 228 |
throw new Error( |
| 229 |
'Can\'t combine significant digits and minimum fraction digits'); |
| 230 |
} |
| 231 |
this.minimumFractionDigits_ = min; |
| 232 |
return this; |
| 233 |
}; |
| 234 |
|
| 235 |
|
| 236 |
/** |
| 237 |
* Sets maximum number of fraction digits. |
| 238 |
* @param {number} max the maximum. |
| 239 |
* @return {!goog.i18n.NumberFormat} Reference to this NumberFormat object. |
| 240 |
*/ |
| 241 |
goog.i18n.NumberFormat.prototype.setMaximumFractionDigits = function(max) { |
| 242 |
if (max > 308) { |
| 243 |
// Math.pow(10, 309) becomes Infinity which breaks the logic in this class. |
| 244 |
throw new Error('Unsupported maximum fraction digits: ' + max); |
| 245 |
} |
| 246 |
this.maximumFractionDigits_ = max; |
| 247 |
return this; |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Sets number of significant digits to show. Only fractions will be rounded. |
| 253 |
* Regardless of the number of significant digits set, the number of fractional |
| 254 |
* digits shown will always be capped by the maximum number of fractional digits |
| 255 |
* set on {@link #setMaximumFractionDigits}. |
| 256 |
* @param {number} number The number of significant digits to include. |
| 257 |
* @return {!goog.i18n.NumberFormat} Reference to this NumberFormat object. |
| 258 |
*/ |
| 259 |
goog.i18n.NumberFormat.prototype.setSignificantDigits = function(number) { |
| 260 |
if (this.minimumFractionDigits_ > 0 && number >= 0) { |
| 261 |
throw new Error( |
| 262 |
'Can\'t combine significant digits and minimum fraction digits'); |
| 263 |
} |
| 264 |
this.significantDigits_ = number; |
| 265 |
return this; |
| 266 |
}; |
| 267 |
|
| 268 |
|
| 269 |
/** |
| 270 |
* Gets number of significant digits to show. Only fractions will be rounded. |
| 271 |
* @return {number} The number of significant digits to include. |
| 272 |
*/ |
| 273 |
goog.i18n.NumberFormat.prototype.getSignificantDigits = function() { |
| 274 |
return this.significantDigits_; |
| 275 |
}; |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Sets whether trailing fraction zeros should be shown when significantDigits_ |
| 280 |
* is positive. If this is true and significantDigits_ is 2, 1 will be formatted |
| 281 |
* as '1.0'. |
| 282 |
* @param {boolean} showTrailingZeros Whether trailing zeros should be shown. |
| 283 |
* @return {!goog.i18n.NumberFormat} Reference to this NumberFormat object. |
| 284 |
*/ |
| 285 |
goog.i18n.NumberFormat.prototype.setShowTrailingZeros = function( |
| 286 |
showTrailingZeros) { |
| 287 |
this.showTrailingZeros_ = showTrailingZeros; |
| 288 |
return this; |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
/** |
| 293 |
* Sets a number to base the formatting on when compact style formatting is |
| 294 |
* used. If this is null, the formatting should be based only on the number to |
| 295 |
* be formatting. |
| 296 |
* |
| 297 |
* This base formatting number can be used to format the target number as |
| 298 |
* another number would be formatted. For example, 100,000 is normally formatted |
| 299 |
* as "100K" in the COMPACT_SHORT format. To instead format it as '0.1M', the |
| 300 |
* base number could be set to 1,000,000 in order to force all numbers to be |
| 301 |
* formatted in millions. Similarly, 1,000,000,000 would normally be formatted |
| 302 |
* as '1B' and setting the base formatting number to 1,000,000, would cause it |
| 303 |
* to be formatted instead as '1,000M'. |
| 304 |
* |
| 305 |
* @param {?number} baseFormattingNumber The number to base formatting on, or |
| 306 |
* null if formatting should not be based on another number. |
| 307 |
* @return {!goog.i18n.NumberFormat} Reference to this NumberFormat object. |
| 308 |
*/ |
| 309 |
goog.i18n.NumberFormat.prototype.setBaseFormatting = function( |
| 310 |
baseFormattingNumber) { |
| 311 |
goog.asserts.assert( |
| 312 |
goog.isNull(baseFormattingNumber) || isFinite(baseFormattingNumber)); |
| 313 |
this.baseFormattingNumber_ = baseFormattingNumber; |
| 314 |
return this; |
| 315 |
}; |
| 316 |
|
| 317 |
|
| 318 |
/** |
| 319 |
* Gets the number on which compact formatting is currently based, or null if |
| 320 |
* no such number is set. See setBaseFormatting() for more information. |
| 321 |
* @return {?number} |
| 322 |
*/ |
| 323 |
goog.i18n.NumberFormat.prototype.getBaseFormatting = function() { |
| 324 |
return this.baseFormattingNumber_; |
| 325 |
}; |
| 326 |
|
| 327 |
|
| 328 |
/** |
| 329 |
* Apply provided pattern, result are stored in member variables. |
| 330 |
* |
| 331 |
* @param {string} pattern String pattern being applied. |
| 332 |
* @private |
| 333 |
*/ |
| 334 |
goog.i18n.NumberFormat.prototype.applyPattern_ = function(pattern) { |
| 335 |
this.pattern_ = pattern.replace(/ /g, '\u00a0'); |
| 336 |
var pos = [0]; |
| 337 |
|
| 338 |
this.positivePrefix_ = this.parseAffix_(pattern, pos); |
| 339 |
var trunkStart = pos[0]; |
| 340 |
this.parseTrunk_(pattern, pos); |
| 341 |
var trunkLen = pos[0] - trunkStart; |
| 342 |
this.positiveSuffix_ = this.parseAffix_(pattern, pos); |
| 343 |
if (pos[0] < pattern.length && |
| 344 |
pattern.charAt(pos[0]) == goog.i18n.NumberFormat.PATTERN_SEPARATOR_) { |
| 345 |
pos[0]++; |
| 346 |
if (this.multiplier_ != 1) this.negativePercentSignExpected_ = true; |
| 347 |
this.negativePrefix_ = this.parseAffix_(pattern, pos); |
| 348 |
// we assume this part is identical to positive part. |
| 349 |
// user must make sure the pattern is correctly constructed. |
| 350 |
pos[0] += trunkLen; |
| 351 |
this.negativeSuffix_ = this.parseAffix_(pattern, pos); |
| 352 |
} else { |
| 353 |
// if no negative affix specified, they share the same positive affix |
| 354 |
this.negativePrefix_ += this.positivePrefix_; |
| 355 |
this.negativeSuffix_ += this.positiveSuffix_; |
| 356 |
} |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
/** |
| 361 |
* Apply a predefined pattern to NumberFormat object. |
| 362 |
* @param {number} patternType The number that indicates a predefined number |
| 363 |
* format pattern. |
| 364 |
* @private |
| 365 |
*/ |
| 366 |
goog.i18n.NumberFormat.prototype.applyStandardPattern_ = function(patternType) { |
| 367 |
switch (patternType) { |
| 368 |
case goog.i18n.NumberFormat.Format.DECIMAL: |
| 369 |
this.applyPattern_( |
| 370 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().DECIMAL_PATTERN); |
| 371 |
break; |
| 372 |
case goog.i18n.NumberFormat.Format.SCIENTIFIC: |
| 373 |
this.applyPattern_( |
| 374 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().SCIENTIFIC_PATTERN); |
| 375 |
break; |
| 376 |
case goog.i18n.NumberFormat.Format.PERCENT: |
| 377 |
this.applyPattern_( |
| 378 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().PERCENT_PATTERN); |
| 379 |
break; |
| 380 |
case goog.i18n.NumberFormat.Format.CURRENCY: |
| 381 |
this.applyPattern_(goog.i18n.currency.adjustPrecision( |
| 382 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().CURRENCY_PATTERN, |
| 383 |
this.getCurrencyCode_())); |
| 384 |
break; |
| 385 |
case goog.i18n.NumberFormat.Format.COMPACT_SHORT: |
| 386 |
this.applyCompactStyle_(goog.i18n.NumberFormat.CompactStyle.SHORT); |
| 387 |
break; |
| 388 |
case goog.i18n.NumberFormat.Format.COMPACT_LONG: |
| 389 |
this.applyCompactStyle_(goog.i18n.NumberFormat.CompactStyle.LONG); |
| 390 |
break; |
| 391 |
default: |
| 392 |
throw new Error('Unsupported pattern type.'); |
| 393 |
} |
| 394 |
}; |
| 395 |
|
| 396 |
|
| 397 |
/** |
| 398 |
* Apply a predefined pattern for shorthand formats. |
| 399 |
* @param {goog.i18n.NumberFormat.CompactStyle} style the compact style to |
| 400 |
* set defaults for. |
| 401 |
* @private |
| 402 |
*/ |
| 403 |
goog.i18n.NumberFormat.prototype.applyCompactStyle_ = function(style) { |
| 404 |
this.compactStyle_ = style; |
| 405 |
this.applyPattern_( |
| 406 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().DECIMAL_PATTERN); |
| 407 |
this.setMinimumFractionDigits(0); |
| 408 |
this.setMaximumFractionDigits(2); |
| 409 |
this.setSignificantDigits(2); |
| 410 |
}; |
| 411 |
|
| 412 |
|
| 413 |
/** |
| 414 |
* Parses text string to produce a Number. |
| 415 |
* |
| 416 |
* This method attempts to parse text starting from position "opt_pos" if it |
| 417 |
* is given. Otherwise the parse will start from the beginning of the text. |
| 418 |
* When opt_pos presents, opt_pos will be updated to the character next to where |
| 419 |
* parsing stops after the call. If an error occurs, opt_pos won't be updated. |
| 420 |
* |
| 421 |
* @param {string} text The string to be parsed. |
| 422 |
* @param {Array<number>=} opt_pos Position to pass in and get back. |
| 423 |
* @return {number} Parsed number. This throws an error if the text cannot be |
| 424 |
* parsed. |
| 425 |
*/ |
| 426 |
goog.i18n.NumberFormat.prototype.parse = function(text, opt_pos) { |
| 427 |
var pos = opt_pos || [0]; |
| 428 |
|
| 429 |
if (this.compactStyle_ != goog.i18n.NumberFormat.CompactStyle.NONE) { |
| 430 |
throw new Error('Parsing of compact numbers is unimplemented'); |
| 431 |
} |
| 432 |
|
| 433 |
var ret = NaN; |
| 434 |
|
| 435 |
// we don't want to handle 2 kind of space in parsing, normalize it to nbsp |
| 436 |
text = text.replace(/ /g, '\u00a0'); |
| 437 |
|
| 438 |
var gotPositive = text.indexOf(this.positivePrefix_, pos[0]) == pos[0]; |
| 439 |
var gotNegative = text.indexOf(this.negativePrefix_, pos[0]) == pos[0]; |
| 440 |
|
| 441 |
// check for the longest match |
| 442 |
if (gotPositive && gotNegative) { |
| 443 |
if (this.positivePrefix_.length > this.negativePrefix_.length) { |
| 444 |
gotNegative = false; |
| 445 |
} else if (this.positivePrefix_.length < this.negativePrefix_.length) { |
| 446 |
gotPositive = false; |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
if (gotPositive) { |
| 451 |
pos[0] += this.positivePrefix_.length; |
| 452 |
} else if (gotNegative) { |
| 453 |
pos[0] += this.negativePrefix_.length; |
| 454 |
} |
| 455 |
|
| 456 |
// process digits or Inf, find decimal position |
| 457 |
if (text.indexOf( |
| 458 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().INFINITY, pos[0]) == |
| 459 |
pos[0]) { |
| 460 |
pos[0] += goog.i18n.NumberFormat.getNumberFormatSymbols_().INFINITY.length; |
| 461 |
ret = Infinity; |
| 462 |
} else { |
| 463 |
ret = this.parseNumber_(text, pos); |
| 464 |
} |
| 465 |
|
| 466 |
// check for suffix |
| 467 |
if (gotPositive) { |
| 468 |
if (!(text.indexOf(this.positiveSuffix_, pos[0]) == pos[0])) { |
| 469 |
return NaN; |
| 470 |
} |
| 471 |
pos[0] += this.positiveSuffix_.length; |
| 472 |
} else if (gotNegative) { |
| 473 |
if (!(text.indexOf(this.negativeSuffix_, pos[0]) == pos[0])) { |
| 474 |
return NaN; |
| 475 |
} |
| 476 |
pos[0] += this.negativeSuffix_.length; |
| 477 |
} |
| 478 |
|
| 479 |
return gotNegative ? -ret : ret; |
| 480 |
}; |
| 481 |
|
| 482 |
|
| 483 |
/** |
| 484 |
* This function will parse a "localized" text into a Number. It needs to |
| 485 |
* handle locale specific decimal, grouping, exponent and digits. |
| 486 |
* |
| 487 |
* @param {string} text The text that need to be parsed. |
| 488 |
* @param {Array<number>} pos In/out parsing position. In case of failure, |
| 489 |
* pos value won't be changed. |
| 490 |
* @return {number} Number value, or NaN if nothing can be parsed. |
| 491 |
* @private |
| 492 |
*/ |
| 493 |
goog.i18n.NumberFormat.prototype.parseNumber_ = function(text, pos) { |
| 494 |
var sawDecimal = false; |
| 495 |
var sawExponent = false; |
| 496 |
var sawDigit = false; |
| 497 |
var exponentPos = -1; |
| 498 |
var scale = 1; |
| 499 |
var decimal = goog.i18n.NumberFormat.getNumberFormatSymbols_().DECIMAL_SEP; |
| 500 |
var grouping = goog.i18n.NumberFormat.getNumberFormatSymbols_().GROUP_SEP; |
| 501 |
var exponentChar = |
| 502 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().EXP_SYMBOL; |
| 503 |
|
| 504 |
if (this.compactStyle_ != goog.i18n.NumberFormat.CompactStyle.NONE) { |
| 505 |
throw new Error('Parsing of compact style numbers is not implemented'); |
| 506 |
} |
| 507 |
|
| 508 |
var normalizedText = ''; |
| 509 |
for (; pos[0] < text.length; pos[0]++) { |
| 510 |
var ch = text.charAt(pos[0]); |
| 511 |
var digit = this.getDigit_(ch); |
| 512 |
if (digit >= 0 && digit <= 9) { |
| 513 |
normalizedText += digit; |
| 514 |
sawDigit = true; |
| 515 |
} else if (ch == decimal.charAt(0)) { |
| 516 |
if (sawDecimal || sawExponent) { |
| 517 |
break; |
| 518 |
} |
| 519 |
normalizedText += '.'; |
| 520 |
sawDecimal = true; |
| 521 |
} else if ( |
| 522 |
ch == grouping.charAt(0) && |
| 523 |
('\u00a0' != grouping.charAt(0) || |
| 524 |
pos[0] + 1 < text.length && |
| 525 |
this.getDigit_(text.charAt(pos[0] + 1)) >= 0)) { |
| 526 |
// Got a grouping character here. When grouping character is nbsp, need |
| 527 |
// to make sure the character following it is a digit. |
| 528 |
if (sawDecimal || sawExponent) { |
| 529 |
break; |
| 530 |
} |
| 531 |
continue; |
| 532 |
} else if (ch == exponentChar.charAt(0)) { |
| 533 |
if (sawExponent) { |
| 534 |
break; |
| 535 |
} |
| 536 |
normalizedText += 'E'; |
| 537 |
sawExponent = true; |
| 538 |
exponentPos = pos[0]; |
| 539 |
} else if (ch == '+' || ch == '-') { |
| 540 |
// Stop parsing if a '+' or '-' sign is found after digits have been found |
| 541 |
// but it's not located right after an exponent sign. |
| 542 |
if (sawDigit && exponentPos != pos[0] - 1) { |
| 543 |
break; |
| 544 |
} |
| 545 |
normalizedText += ch; |
| 546 |
} else if ( |
| 547 |
this.multiplier_ == 1 && |
| 548 |
ch == |
| 549 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().PERCENT.charAt( |
| 550 |
0)) { |
| 551 |
// Parse the percent character as part of the number only when it's |
| 552 |
// not already included in the pattern. |
| 553 |
if (scale != 1) { |
| 554 |
break; |
| 555 |
} |
| 556 |
scale = 100; |
| 557 |
if (sawDigit) { |
| 558 |
pos[0]++; // eat this character if parse end here |
| 559 |
break; |
| 560 |
} |
| 561 |
} else if ( |
| 562 |
this.multiplier_ == 1 && |
| 563 |
ch == |
| 564 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().PERMILL.charAt( |
| 565 |
0)) { |
| 566 |
// Parse the permill character as part of the number only when it's |
| 567 |
// not already included in the pattern. |
| 568 |
if (scale != 1) { |
| 569 |
break; |
| 570 |
} |
| 571 |
scale = 1000; |
| 572 |
if (sawDigit) { |
| 573 |
pos[0]++; // eat this character if parse end here |
| 574 |
break; |
| 575 |
} |
| 576 |
} else { |
| 577 |
break; |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
// Scale the number when the percent/permill character was included in |
| 582 |
// the pattern. |
| 583 |
if (this.multiplier_ != 1) { |
| 584 |
scale = this.multiplier_; |
| 585 |
} |
| 586 |
|
| 587 |
return parseFloat(normalizedText) / scale; |
| 588 |
}; |
| 589 |
|
| 590 |
|
| 591 |
/** |
| 592 |
* Formats a Number to produce a string. |
| 593 |
* |
| 594 |
* @param {number} number The Number to be formatted. |
| 595 |
* @return {string} The formatted number string. |
| 596 |
*/ |
| 597 |
goog.i18n.NumberFormat.prototype.format = function(number) { |
| 598 |
if (isNaN(number)) { |
| 599 |
return goog.i18n.NumberFormat.getNumberFormatSymbols_().NAN; |
| 600 |
} |
| 601 |
|
| 602 |
var parts = []; |
| 603 |
var baseFormattingNumber = goog.isNull(this.baseFormattingNumber_) ? |
| 604 |
number : |
| 605 |
this.baseFormattingNumber_; |
| 606 |
var unit = this.getUnitAfterRounding_(baseFormattingNumber, number); |
| 607 |
number /= Math.pow(10, unit.divisorBase); |
| 608 |
|
| 609 |
parts.push(unit.prefix); |
| 610 |
|
| 611 |
// in icu code, it is commented that certain computation need to keep the |
| 612 |
// negative sign for 0. |
| 613 |
var isNegative = number < 0.0 || number == 0.0 && 1 / number < 0.0; |
| 614 |
|
| 615 |
parts.push(isNegative ? this.negativePrefix_ : this.positivePrefix_); |
| 616 |
|
| 617 |
if (!isFinite(number)) { |
| 618 |
parts.push(goog.i18n.NumberFormat.getNumberFormatSymbols_().INFINITY); |
| 619 |
} else { |
| 620 |
// convert number to non-negative value |
| 621 |
number *= isNegative ? -1 : 1; |
| 622 |
|
| 623 |
number *= this.multiplier_; |
| 624 |
this.useExponentialNotation_ ? |
| 625 |
this.subformatExponential_(number, parts) : |
| 626 |
this.subformatFixed_(number, this.minimumIntegerDigits_, parts); |
| 627 |
} |
| 628 |
|
| 629 |
parts.push(isNegative ? this.negativeSuffix_ : this.positiveSuffix_); |
| 630 |
parts.push(unit.suffix); |
| 631 |
|
| 632 |
return parts.join(''); |
| 633 |
}; |
| 634 |
|
| 635 |
|
| 636 |
/** |
| 637 |
* Round a number into an integer and fractional part |
| 638 |
* based on the rounding rules for this NumberFormat. |
| 639 |
* @param {number} number The number to round. |
| 640 |
* @return {{intValue: number, fracValue: number}} The integer and fractional |
| 641 |
* part after rounding. |
| 642 |
* @private |
| 643 |
*/ |
| 644 |
goog.i18n.NumberFormat.prototype.roundNumber_ = function(number) { |
| 645 |
var power = Math.pow(10, this.maximumFractionDigits_); |
| 646 |
var shiftedNumber = this.significantDigits_ <= 0 ? |
| 647 |
Math.round(number * power) : |
| 648 |
Math.round( |
| 649 |
this.roundToSignificantDigits_( |
| 650 |
number * power, this.significantDigits_, |
| 651 |
this.maximumFractionDigits_)); |
| 652 |
|
| 653 |
var intValue, fracValue; |
| 654 |
if (isFinite(shiftedNumber)) { |
| 655 |
intValue = Math.floor(shiftedNumber / power); |
| 656 |
fracValue = Math.floor(shiftedNumber - intValue * power); |
| 657 |
} else { |
| 658 |
intValue = number; |
| 659 |
fracValue = 0; |
| 660 |
} |
| 661 |
return {intValue: intValue, fracValue: fracValue}; |
| 662 |
}; |
| 663 |
|
| 664 |
|
| 665 |
/** |
| 666 |
* Formats a number with the appropriate groupings when there are repeating |
| 667 |
* digits present. Repeating digits exists when the length of the digits left |
| 668 |
* of the decimal place exceeds the number of non-repeating digits. |
| 669 |
* |
| 670 |
* Formats a number by iterating through the integer number (intPart) from the |
| 671 |
* most left of the decimal place by inserting the appropriate number grouping |
| 672 |
* separator for the repeating digits until all of the repeating digits is |
| 673 |
* iterated. Then iterate through the non-repeating digits by inserting the |
| 674 |
* appropriate number grouping separator until all the non-repeating digits |
| 675 |
* is iterated through. |
| 676 |
* |
| 677 |
* In the number grouping concept, anything left of the decimal |
| 678 |
* place is followed by non-repeating digits and then repeating digits. If the |
| 679 |
* pattern is #,##,###, then we first (from the left of the decimal place) have |
| 680 |
* a non-repeating digit of size 3 followed by repeating digits of size 2 |
| 681 |
* separated by a thousand separator. If the length of the digits are six or |
| 682 |
* more, there may be repeating digits required. For example, the value of |
| 683 |
* 12345678 would format as 1,23,45,678 where the repeating digit is length 2. |
| 684 |
* |
| 685 |
* @param {!Array<string>} parts An array to build the 'parts' of the formatted |
| 686 |
* number including the values and separators. |
| 687 |
* @param {number} zeroCode The value of the zero digit whether or not |
| 688 |
* goog.i18n.NumberFormat.enforceAsciiDigits_ is enforced. |
| 689 |
* @param {string} intPart The integer representation of the number to be |
| 690 |
* formatted and referenced. |
| 691 |
* @param {!Array<number>} groupingArray The array of numbers to determine the |
| 692 |
* grouping of repeated and non-repeated digits. |
| 693 |
* @param {number} repeatedDigitLen The length of the repeated digits left of |
| 694 |
* the non-repeating digits left of the decimal. |
| 695 |
* @return {!Array<string>} Returns the resulting parts variable containing |
| 696 |
* how numbers are to be grouped and appear. |
| 697 |
* @private |
| 698 |
*/ |
| 699 |
goog.i18n.NumberFormat.formatNumberGroupingRepeatingDigitsParts_ = function( |
| 700 |
parts, zeroCode, intPart, groupingArray, repeatedDigitLen) { |
| 701 |
// Keep track of how much has been completed on the non repeated groups |
| 702 |
var nonRepeatedGroupCompleteCount = 0; |
| 703 |
var currentGroupSizeIndex = 0; |
| 704 |
var currentGroupSize = 0; |
| 705 |
|
| 706 |
var grouping = goog.i18n.NumberFormat.getNumberFormatSymbols_().GROUP_SEP; |
| 707 |
var digitLen = intPart.length; |
| 708 |
|
| 709 |
// There are repeating digits and non-repeating digits |
| 710 |
for (var i = 0; i < digitLen; i++) { |
| 711 |
parts.push(String.fromCharCode(zeroCode + Number(intPart.charAt(i)) * 1)); |
| 712 |
if (digitLen - i > 1) { |
| 713 |
currentGroupSize = groupingArray[currentGroupSizeIndex]; |
| 714 |
if (i < repeatedDigitLen) { |
| 715 |
// Process the left side (the repeated number groups) |
| 716 |
var repeatedDigitIndex = repeatedDigitLen - i; |
| 717 |
// Edge case if there's a number grouping asking for "1" group at |
| 718 |
// a time; otherwise, if the remainder is 1, there's the separator |
| 719 |
if (currentGroupSize === 1 || |
| 720 |
(currentGroupSize > 0 && |
| 721 |
(repeatedDigitIndex % currentGroupSize) === 1)) { |
| 722 |
parts.push(grouping); |
| 723 |
} |
| 724 |
} else if (currentGroupSizeIndex < groupingArray.length) { |
| 725 |
// Process the right side (the non-repeated fixed number groups) |
| 726 |
if (i === repeatedDigitLen) { |
| 727 |
// Increase the group index because a separator |
| 728 |
// has previously added in the earlier logic |
| 729 |
currentGroupSizeIndex += 1; |
| 730 |
} else if ( |
| 731 |
currentGroupSize === |
| 732 |
i - repeatedDigitLen - nonRepeatedGroupCompleteCount + 1) { |
| 733 |
// Otherwise, just iterate to the right side and |
| 734 |
// add a separator once the length matches to the expected |
| 735 |
parts.push(grouping); |
| 736 |
// Keep track of what has been completed on the right |
| 737 |
nonRepeatedGroupCompleteCount += currentGroupSize; |
| 738 |
currentGroupSizeIndex += 1; // Get to the next number grouping |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
} |
| 743 |
return parts; |
| 744 |
}; |
| 745 |
|
| 746 |
|
| 747 |
/** |
| 748 |
* Formats a number with the appropriate groupings when there are no repeating |
| 749 |
* digits present. Non-repeating digits exists when the length of the digits |
| 750 |
* left of the decimal place is equal or lesser than the length of |
| 751 |
* non-repeating digits. |
| 752 |
* |
| 753 |
* Formats a number by iterating through the integer number (intPart) from the |
| 754 |
* right most non-repeating number group of the decimal place. For each group, |
| 755 |
* inserting the appropriate number grouping separator for the non-repeating |
| 756 |
* digits until the number is completely iterated. |
| 757 |
* |
| 758 |
* In the number grouping concept, anything left of the decimal |
| 759 |
* place is followed by non-repeating digits and then repeating digits. If the |
| 760 |
* pattern is #,##,###, then we first (from the left of the decimal place) have |
| 761 |
* a non-repeating digit of size 3 followed by repeating digits of size 2 |
| 762 |
* separated by a thousand separator. If the length of the digits are five or |
| 763 |
* less, there won't be any repeating digits required. For example, the value |
| 764 |
* of 12345 would be formatted as 12,345 where the non-repeating digit is of |
| 765 |
* length 3. |
| 766 |
* |
| 767 |
* @param {!Array<string>} parts An array to build the 'parts' of the formatted |
| 768 |
* number including the values and separators. |
| 769 |
* @param {number} zeroCode The value of the zero digit whether or not |
| 770 |
* goog.i18n.NumberFormat.enforceAsciiDigits_ is enforced. |
| 771 |
* @param {string} intPart The integer representation of the number to be |
| 772 |
* formatted and referenced. |
| 773 |
* @param {!Array<number>} groupingArray The array of numbers to determine the |
| 774 |
* grouping of repeated and non-repeated digits. |
| 775 |
* @return {!Array<string>} Returns the resulting parts variable containing |
| 776 |
* how numbers are to be grouped and appear. |
| 777 |
* @private |
| 778 |
*/ |
| 779 |
goog.i18n.NumberFormat.formatNumberGroupingNonRepeatingDigitsParts_ = function( |
| 780 |
parts, zeroCode, intPart, groupingArray) { |
| 781 |
// Keep track of how much has been completed on the non repeated groups |
| 782 |
var grouping = goog.i18n.NumberFormat.getNumberFormatSymbols_().GROUP_SEP; |
| 783 |
var currentGroupSizeIndex; |
| 784 |
var currentGroupSize = 0; |
| 785 |
var digitLenLeft = intPart.length; |
| 786 |
var rightToLeftParts = []; |
| 787 |
|
| 788 |
// Start from the right most non-repeating group and work inwards |
| 789 |
for (currentGroupSizeIndex = groupingArray.length - 1; |
| 790 |
currentGroupSizeIndex >= 0 && digitLenLeft > 0; |
| 791 |
currentGroupSizeIndex--) { |
| 792 |
currentGroupSize = groupingArray[currentGroupSizeIndex]; |
| 793 |
// Iterate from the right most digit |
| 794 |
for (var rightDigitIndex = 0; rightDigitIndex < currentGroupSize && |
| 795 |
((digitLenLeft - rightDigitIndex - 1) >= 0); |
| 796 |
rightDigitIndex++) { |
| 797 |
rightToLeftParts.push( |
| 798 |
String.fromCharCode( |
| 799 |
zeroCode + |
| 800 |
Number(intPart.charAt(digitLenLeft - rightDigitIndex - 1)) * 1)); |
| 801 |
} |
| 802 |
// Update the number of digits left |
| 803 |
digitLenLeft -= currentGroupSize; |
| 804 |
if (digitLenLeft > 0) { |
| 805 |
rightToLeftParts.push(grouping); |
| 806 |
} |
| 807 |
} |
| 808 |
// Reverse and push onto the remaining parts |
| 809 |
parts.push.apply(parts, rightToLeftParts.reverse()); |
| 810 |
|
| 811 |
return parts; |
| 812 |
}; |
| 813 |
|
| 814 |
|
| 815 |
/** |
| 816 |
* Formats a Number in fraction format. |
| 817 |
* |
| 818 |
* @param {number} number |
| 819 |
* @param {number} minIntDigits Minimum integer digits. |
| 820 |
* @param {Array<string>} parts |
| 821 |
* This array holds the pieces of formatted string. |
| 822 |
* This function will add its formatted pieces to the array. |
| 823 |
* @private |
| 824 |
*/ |
| 825 |
goog.i18n.NumberFormat.prototype.subformatFixed_ = function( |
| 826 |
number, minIntDigits, parts) { |
| 827 |
if (this.minimumFractionDigits_ > this.maximumFractionDigits_) { |
| 828 |
throw new Error('Min value must be less than max value'); |
| 829 |
} |
| 830 |
|
| 831 |
if (!parts) { |
| 832 |
parts = []; |
| 833 |
} |
| 834 |
|
| 835 |
var rounded = this.roundNumber_(number); |
| 836 |
var intValue = rounded.intValue; |
| 837 |
var fracValue = rounded.fracValue; |
| 838 |
|
| 839 |
var numIntDigits = (intValue == 0) ? 0 : this.intLog10_(intValue) + 1; |
| 840 |
var fractionPresent = this.minimumFractionDigits_ > 0 || fracValue > 0 || |
| 841 |
(this.showTrailingZeros_ && numIntDigits < this.significantDigits_); |
| 842 |
var minimumFractionDigits = this.minimumFractionDigits_; |
| 843 |
if (fractionPresent) { |
| 844 |
if (this.showTrailingZeros_ && this.significantDigits_ > 0) { |
| 845 |
minimumFractionDigits = this.significantDigits_ - numIntDigits; |
| 846 |
} else { |
| 847 |
minimumFractionDigits = this.minimumFractionDigits_; |
| 848 |
} |
| 849 |
} |
| 850 |
|
| 851 |
var intPart = ''; |
| 852 |
var translatableInt = intValue; |
| 853 |
while (translatableInt > 1E20) { |
| 854 |
// here it goes beyond double precision, add '0' make it look better |
| 855 |
intPart = '0' + intPart; |
| 856 |
translatableInt = Math.round(translatableInt / 10); |
| 857 |
} |
| 858 |
intPart = translatableInt + intPart; |
| 859 |
|
| 860 |
var decimal = goog.i18n.NumberFormat.getNumberFormatSymbols_().DECIMAL_SEP; |
| 861 |
var zeroCode = |
| 862 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().ZERO_DIGIT.charCodeAt(0); |
| 863 |
var digitLen = intPart.length; |
| 864 |
var nonRepeatedGroupCount = 0; |
| 865 |
|
| 866 |
if (intValue > 0 || minIntDigits > 0) { |
| 867 |
for (var i = digitLen; i < minIntDigits; i++) { |
| 868 |
parts.push(String.fromCharCode(zeroCode)); |
| 869 |
} |
| 870 |
|
| 871 |
// If there's more than 1 number grouping, |
| 872 |
// figure out the length of the non-repeated groupings (on the right) |
| 873 |
if (this.groupingArray_.length >= 2) { |
| 874 |
for (var j = 1; j < this.groupingArray_.length; j++) { |
| 875 |
nonRepeatedGroupCount += this.groupingArray_[j]; |
| 876 |
} |
| 877 |
} |
| 878 |
|
| 879 |
// Anything left of the fixed number grouping is repeated, |
| 880 |
// figure out the length of repeated groupings (on the left) |
| 881 |
var repeatedDigitLen = digitLen - nonRepeatedGroupCount; |
| 882 |
if (repeatedDigitLen > 0) { |
| 883 |
// There are repeating digits and non-repeating digits |
| 884 |
parts = goog.i18n.NumberFormat.formatNumberGroupingRepeatingDigitsParts_( |
| 885 |
parts, zeroCode, intPart, this.groupingArray_, repeatedDigitLen); |
| 886 |
} else { |
| 887 |
// There are no repeating digits and only non-repeating digits |
| 888 |
parts = |
| 889 |
goog.i18n.NumberFormat.formatNumberGroupingNonRepeatingDigitsParts_( |
| 890 |
parts, zeroCode, intPart, this.groupingArray_); |
| 891 |
} |
| 892 |
} else if (!fractionPresent) { |
| 893 |
// If there is no fraction present, and we haven't printed any |
| 894 |
// integer digits, then print a zero. |
| 895 |
parts.push(String.fromCharCode(zeroCode)); |
| 896 |
} |
| 897 |
|
| 898 |
// Output the decimal separator if we always do so. |
| 899 |
if (this.decimalSeparatorAlwaysShown_ || fractionPresent) { |
| 900 |
parts.push(decimal); |
| 901 |
} |
| 902 |
|
| 903 |
var fracPart = String(fracValue); |
| 904 |
// Handle case where fracPart is in scientific notation. |
| 905 |
var fracPartSplit = fracPart.split('e+'); |
| 906 |
if (fracPartSplit.length == 2) { |
| 907 |
// Only keep significant digits. |
| 908 |
var floatFrac = parseFloat(fracPartSplit[0]); |
| 909 |
fracPart = String( |
| 910 |
this.roundToSignificantDigits_(floatFrac, this.significantDigits_, 1)); |
| 911 |
fracPart = fracPart.replace('.', ''); |
| 912 |
// Append zeroes based on the exponent. |
| 913 |
var exp = parseInt(fracPartSplit[1], 10); |
| 914 |
fracPart += goog.string.repeat('0', exp - fracPart.length + 1); |
| 915 |
} |
| 916 |
|
| 917 |
// Add Math.pow(10, this.maximumFractionDigits) to fracPart. Uses string ops |
| 918 |
// to avoid complexity with scientific notation and overflows. |
| 919 |
if (this.maximumFractionDigits_ + 1 > fracPart.length) { |
| 920 |
var zeroesToAdd = this.maximumFractionDigits_ - fracPart.length; |
| 921 |
fracPart = '1' + goog.string.repeat('0', zeroesToAdd) + fracPart; |
| 922 |
} |
| 923 |
|
| 924 |
var fracLen = fracPart.length; |
| 925 |
while (fracPart.charAt(fracLen - 1) == '0' && |
| 926 |
fracLen > minimumFractionDigits + 1) { |
| 927 |
fracLen--; |
| 928 |
} |
| 929 |
|
| 930 |
for (var i = 1; i < fracLen; i++) { |
| 931 |
parts.push(String.fromCharCode(zeroCode + Number(fracPart.charAt(i)) * 1)); |
| 932 |
} |
| 933 |
}; |
| 934 |
|
| 935 |
|
| 936 |
/** |
| 937 |
* Formats exponent part of a Number. |
| 938 |
* |
| 939 |
* @param {number} exponent Exponential value. |
| 940 |
* @param {Array<string>} parts The array that holds the pieces of formatted |
| 941 |
* string. This function will append more formatted pieces to the array. |
| 942 |
* @private |
| 943 |
*/ |
| 944 |
goog.i18n.NumberFormat.prototype.addExponentPart_ = function(exponent, parts) { |
| 945 |
parts.push(goog.i18n.NumberFormat.getNumberFormatSymbols_().EXP_SYMBOL); |
| 946 |
|
| 947 |
if (exponent < 0) { |
| 948 |
exponent = -exponent; |
| 949 |
parts.push(goog.i18n.NumberFormat.getNumberFormatSymbols_().MINUS_SIGN); |
| 950 |
} else if (this.useSignForPositiveExponent_) { |
| 951 |
parts.push(goog.i18n.NumberFormat.getNumberFormatSymbols_().PLUS_SIGN); |
| 952 |
} |
| 953 |
|
| 954 |
var exponentDigits = '' + exponent; |
| 955 |
var zeroChar = goog.i18n.NumberFormat.getNumberFormatSymbols_().ZERO_DIGIT; |
| 956 |
for (var i = exponentDigits.length; i < this.minExponentDigits_; i++) { |
| 957 |
parts.push(zeroChar); |
| 958 |
} |
| 959 |
parts.push(exponentDigits); |
| 960 |
}; |
| 961 |
|
| 962 |
/** |
| 963 |
* Returns the mantissa for the given value and its exponent. |
| 964 |
* |
| 965 |
* @param {number} value |
| 966 |
* @param {number} exponent |
| 967 |
* @return {number} |
| 968 |
* @private |
| 969 |
*/ |
| 970 |
goog.i18n.NumberFormat.prototype.getMantissa_ = function(value, exponent) { |
| 971 |
var divisor = Math.pow(10, exponent); |
| 972 |
if (isFinite(divisor) && divisor !== 0) { |
| 973 |
return value / divisor; |
| 974 |
} else { |
| 975 |
// If the exponent is too big pow returns 0. In such a case we calculate |
| 976 |
// half of the divisor and apply it twice. |
| 977 |
divisor = Math.pow(10, Math.floor(exponent / 2)); |
| 978 |
var result = value / divisor / divisor; |
| 979 |
if (exponent % 2 == 1) { // Correcting for odd exponents. |
| 980 |
if (exponent > 0) { |
| 981 |
result /= 10; |
| 982 |
} else { |
| 983 |
result *= 10; |
| 984 |
} |
| 985 |
} |
| 986 |
return result; |
| 987 |
} |
| 988 |
}; |
| 989 |
|
| 990 |
/** |
| 991 |
* Formats Number in exponential format. |
| 992 |
* |
| 993 |
* @param {number} number Value need to be formatted. |
| 994 |
* @param {Array<string>} parts The array that holds the pieces of formatted |
| 995 |
* string. This function will append more formatted pieces to the array. |
| 996 |
* @private |
| 997 |
*/ |
| 998 |
goog.i18n.NumberFormat.prototype.subformatExponential_ = function( |
| 999 |
number, parts) { |
| 1000 |
if (number == 0.0) { |
| 1001 |
this.subformatFixed_(number, this.minimumIntegerDigits_, parts); |
| 1002 |
this.addExponentPart_(0, parts); |
| 1003 |
return; |
| 1004 |
} |
| 1005 |
|
| 1006 |
var exponent = goog.math.safeFloor(Math.log(number) / Math.log(10)); |
| 1007 |
number = this.getMantissa_(number, exponent); |
| 1008 |
|
| 1009 |
var minIntDigits = this.minimumIntegerDigits_; |
| 1010 |
if (this.maximumIntegerDigits_ > 1 && |
| 1011 |
this.maximumIntegerDigits_ > this.minimumIntegerDigits_) { |
| 1012 |
// A repeating range is defined; adjust to it as follows. |
| 1013 |
// If repeat == 3, we have 6,5,4=>3; 3,2,1=>0; 0,-1,-2=>-3; |
| 1014 |
// -3,-4,-5=>-6, etc. This takes into account that the |
| 1015 |
// exponent we have here is off by one from what we expect; |
| 1016 |
// it is for the format 0.MMMMMx10^n. |
| 1017 |
while ((exponent % this.maximumIntegerDigits_) != 0) { |
| 1018 |
number *= 10; |
| 1019 |
exponent--; |
| 1020 |
} |
| 1021 |
minIntDigits = 1; |
| 1022 |
} else { |
| 1023 |
// No repeating range is defined; use minimum integer digits. |
| 1024 |
if (this.minimumIntegerDigits_ < 1) { |
| 1025 |
exponent++; |
| 1026 |
number /= 10; |
| 1027 |
} else { |
| 1028 |
exponent -= this.minimumIntegerDigits_ - 1; |
| 1029 |
number *= Math.pow(10, this.minimumIntegerDigits_ - 1); |
| 1030 |
} |
| 1031 |
} |
| 1032 |
this.subformatFixed_(number, minIntDigits, parts); |
| 1033 |
this.addExponentPart_(exponent, parts); |
| 1034 |
}; |
| 1035 |
|
| 1036 |
|
| 1037 |
/** |
| 1038 |
* Returns the digit value of current character. The character could be either |
| 1039 |
* '0' to '9', or a locale specific digit. |
| 1040 |
* |
| 1041 |
* @param {string} ch Character that represents a digit. |
| 1042 |
* @return {number} The digit value, or -1 on error. |
| 1043 |
* @private |
| 1044 |
*/ |
| 1045 |
goog.i18n.NumberFormat.prototype.getDigit_ = function(ch) { |
| 1046 |
var code = ch.charCodeAt(0); |
| 1047 |
// between '0' to '9' |
| 1048 |
if (48 <= code && code < 58) { |
| 1049 |
return code - 48; |
| 1050 |
} else { |
| 1051 |
var zeroCode = |
| 1052 |
goog.i18n.NumberFormat.getNumberFormatSymbols_().ZERO_DIGIT.charCodeAt( |
| 1053 |
0); |
| 1054 |
return zeroCode <= code && code < zeroCode + 10 ? code - zeroCode : -1; |
| 1055 |
} |
| 1056 |
}; |
| 1057 |
|
| 1058 |
|
| 1059 |
// ---------------------------------------------------------------------- |
| 1060 |
// CONSTANTS |
| 1061 |
// ---------------------------------------------------------------------- |
| 1062 |
// Constants for characters used in programmatic (unlocalized) patterns. |
| 1063 |
/** |
| 1064 |
* A zero digit character. |
| 1065 |
* @type {string} |
| 1066 |
* @private |
| 1067 |
*/ |
| 1068 |
goog.i18n.NumberFormat.PATTERN_ZERO_DIGIT_ = '0'; |
| 1069 |
|
| 1070 |
|
| 1071 |
/** |
| 1072 |
* A grouping separator character. |
| 1073 |
* @type {string} |
| 1074 |
* @private |
| 1075 |
*/ |
| 1076 |
goog.i18n.NumberFormat.PATTERN_GROUPING_SEPARATOR_ = ','; |
| 1077 |
|
| 1078 |
|
| 1079 |
/** |
| 1080 |
* A decimal separator character. |
| 1081 |
* @type {string} |
| 1082 |
* @private |
| 1083 |
*/ |
| 1084 |
goog.i18n.NumberFormat.PATTERN_DECIMAL_SEPARATOR_ = '.'; |
| 1085 |
|
| 1086 |
|
| 1087 |
/** |
| 1088 |
* A per mille character. |
| 1089 |
* @type {string} |
| 1090 |
* @private |
| 1091 |
*/ |
| 1092 |
goog.i18n.NumberFormat.PATTERN_PER_MILLE_ = '\u2030'; |
| 1093 |
|
| 1094 |
|
| 1095 |
/** |
| 1096 |
* A percent character. |
| 1097 |
* @type {string} |
| 1098 |
* @private |
| 1099 |
*/ |
| 1100 |
goog.i18n.NumberFormat.PATTERN_PERCENT_ = '%'; |
| 1101 |
|
| 1102 |
|
| 1103 |
/** |
| 1104 |
* A digit character. |
| 1105 |
* @type {string} |
| 1106 |
* @private |
| 1107 |
*/ |
| 1108 |
goog.i18n.NumberFormat.PATTERN_DIGIT_ = '#'; |
| 1109 |
|
| 1110 |
|
| 1111 |
/** |
| 1112 |
* A separator character. |
| 1113 |
* @type {string} |
| 1114 |
* @private |
| 1115 |
*/ |
| 1116 |
goog.i18n.NumberFormat.PATTERN_SEPARATOR_ = ';'; |
| 1117 |
|
| 1118 |
|
| 1119 |
/** |
| 1120 |
* An exponent character. |
| 1121 |
* @type {string} |
| 1122 |
* @private |
| 1123 |
*/ |
| 1124 |
goog.i18n.NumberFormat.PATTERN_EXPONENT_ = 'E'; |
| 1125 |
|
| 1126 |
|
| 1127 |
/** |
| 1128 |
* A plus character. |
| 1129 |
* @type {string} |
| 1130 |
* @private |
| 1131 |
*/ |
| 1132 |
goog.i18n.NumberFormat.PATTERN_PLUS_ = '+'; |
| 1133 |
|
| 1134 |
|
| 1135 |
/** |
| 1136 |
* A generic currency sign character. |
| 1137 |
* @type {string} |
| 1138 |
* @private |
| 1139 |
*/ |
| 1140 |
goog.i18n.NumberFormat.PATTERN_CURRENCY_SIGN_ = '\u00A4'; |
| 1141 |
|
| 1142 |
|
| 1143 |
/** |
| 1144 |
* A quote character. |
| 1145 |
* @type {string} |
| 1146 |
* @private |
| 1147 |
*/ |
| 1148 |
goog.i18n.NumberFormat.QUOTE_ = '\''; |
| 1149 |
|
| 1150 |
|
| 1151 |
/** |
| 1152 |
* Parses affix part of pattern. |
| 1153 |
* |
| 1154 |
* @param {string} pattern Pattern string that need to be parsed. |
| 1155 |
* @param {Array<number>} pos One element position array to set and receive |
| 1156 |
* parsing position. |
| 1157 |
* |
| 1158 |
* @return {string} Affix received from parsing. |
| 1159 |
* @private |
| 1160 |
*/ |
| 1161 |
goog.i18n.NumberFormat.prototype.parseAffix_ = function(pattern, pos) { |
| 1162 |
var affix = ''; |
| 1163 |
var inQuote = false; |
| 1164 |
var len = pattern.length; |
| 1165 |
|
| 1166 |
for (; pos[0] < len; pos[0]++) { |
| 1167 |
var ch = pattern.charAt(pos[0]); |
| 1168 |
if (ch == goog.i18n.NumberFormat.QUOTE_) { |
| 1169 |
if (pos[0] + 1 < len && |
| 1170 |
pattern.charAt(pos[0] + 1) == goog.i18n.NumberFormat.QUOTE_) { |
| 1171 |
pos[0]++; |
| 1172 |
affix += '\''; // 'don''t' |
| 1173 |
} else { |
| 1174 |
inQuote = !inQuote; |
| 1175 |
} |
| 1176 |
continue; |
| 1177 |
} |
| 1178 |
|
| 1179 |
if (inQuote) { |
| 1180 |
affix += ch; |
| 1181 |
} else { |
| 1182 |
switch (ch) { |
| 1183 |
case goog.i18n.NumberFormat.PATTERN_DIGIT_: |
| 1184 |
case goog.i18n.NumberFormat.PATTERN_ZERO_DIGIT_: |
| 1185 |
case goog.i18n.NumberFormat.PATTERN_GROUPING_SEPARATOR_: |
| 1186 |
case goog.i18n.NumberFormat.PATTERN_DECIMAL_SEPARATOR_: |
| 1187 |
case goog.i18n.NumberFormat.PATTERN_SEPARATOR_: |
| 1188 |
return affix; |
| 1189 |
case goog.i18n.NumberFormat.PATTERN_CURRENCY_SIGN_: |
| 1190 |
if ((pos[0] + 1) < len && |
| 1191 |
pattern.charAt(pos[0] + 1) == |
| 1192 |
goog.i18n.NumberFormat.PATTERN_CURRENCY_SIGN_) { |
| 1193 |
pos[0]++; |
| 1194 |
affix += this.getCurrencyCode_(); |
| 1195 |
} else { |
| 1196 |
switch (this.currencyStyle_) { |
| 1197 |
case goog.i18n.NumberFormat.CurrencyStyle.LOCAL: |
| 1198 |
affix += goog.i18n.currency.getLocalCurrencySign( |
| 1199 |
this.getCurrencyCode_()); |
| 1200 |
break; |
| 1201 |
case goog.i18n.NumberFormat.CurrencyStyle.GLOBAL: |
| 1202 |
affix += goog.i18n.currency.getGlobalCurrencySign( |
| 1203 |
this.getCurrencyCode_()); |
| 1204 |
break; |
| 1205 |
case goog.i18n.NumberFormat.CurrencyStyle.PORTABLE: |
| 1206 |
affix += goog.i18n.currency.getPortableCurrencySign( |
| 1207 |
this.getCurrencyCode_()); |
| 1208 |
break; |
| 1209 |
default: |
| 1210 |
break; |
| 1211 |
} |
| 1212 |
} |
| 1213 |
break; |
| 1214 |
case goog.i18n.NumberFormat.PATTERN_PERCENT_: |
| 1215 |
if (!this.negativePercentSignExpected_ && this.multiplier_ != 1) { |
| 1216 |
throw new Error('Too many percent/permill'); |
| 1217 |
} else if ( |
| 1218 |
this.negativePercentSignExpected_ && this.multiplier_ != 100) { |
| 1219 |
throw new Error('Inconsistent use of percent/permill characters'); |
| 1220 |
} |
| 1221 |
this.multiplier_ = 100; |
| 1222 |
this.negativePercentSignExpected_ = false; |
| 1223 |
affix += goog.i18n.NumberFormat.getNumberFormatSymbols_().PERCENT; |
| 1224 |
break; |
| 1225 |
case goog.i18n.NumberFormat.PATTERN_PER_MILLE_: |
| 1226 |
if (!this.negativePercentSignExpected_ && this.multiplier_ != 1) { |
| 1227 |
throw new Error('Too many percent/permill'); |
| 1228 |
} else if ( |
| 1229 |
this.negativePercentSignExpected_ && this.multiplier_ != 1000) { |
| 1230 |
throw new Error('Inconsistent use of percent/permill characters'); |
| 1231 |
} |
| 1232 |
this.multiplier_ = 1000; |
| 1233 |
this.negativePercentSignExpected_ = false; |
| 1234 |
affix += goog.i18n.NumberFormat.getNumberFormatSymbols_().PERMILL; |
| 1235 |
break; |
| 1236 |
default: |
| 1237 |
affix += ch; |
| 1238 |
} |
| 1239 |
} |
| 1240 |
} |
| 1241 |
|
| 1242 |
return affix; |
| 1243 |
}; |
| 1244 |
|
| 1245 |
|
| 1246 |
/** |
| 1247 |
* Parses the trunk part of a pattern. |
| 1248 |
* |
| 1249 |
* @param {string} pattern Pattern string that need to be parsed. |
| 1250 |
* @param {Array<number>} pos One element position array to set and receive |
| 1251 |
* parsing position. |
| 1252 |
* @private |
| 1253 |
*/ |
| 1254 |
goog.i18n.NumberFormat.prototype.parseTrunk_ = function(pattern, pos) { |
| 1255 |
var decimalPos = -1; |
| 1256 |
var digitLeftCount = 0; |
| 1257 |
var zeroDigitCount = 0; |
| 1258 |
var digitRightCount = 0; |
| 1259 |
var groupingCount = -1; |
| 1260 |
var len = pattern.length; |
| 1261 |
for (var loop = true; pos[0] < len && loop; pos[0]++) { |
| 1262 |
var ch = pattern.charAt(pos[0]); |
| 1263 |
switch (ch) { |
| 1264 |
case goog.i18n.NumberFormat.PATTERN_DIGIT_: |
| 1265 |
if (zeroDigitCount > 0) { |
| 1266 |
digitRightCount++; |
| 1267 |
} else { |
| 1268 |
digitLeftCount++; |
| 1269 |
} |
| 1270 |
if (groupingCount >= 0 && decimalPos < 0) { |
| 1271 |
groupingCount++; |
| 1272 |
} |
| 1273 |
break; |
| 1274 |
case goog.i18n.NumberFormat.PATTERN_ZERO_DIGIT_: |
| 1275 |
if (digitRightCount > 0) { |
| 1276 |
throw new Error('Unexpected "0" in pattern "' + pattern + '"'); |
| 1277 |
} |
| 1278 |
zeroDigitCount++; |
| 1279 |
if (groupingCount >= 0 && decimalPos < 0) { |
| 1280 |
groupingCount++; |
| 1281 |
} |
| 1282 |
break; |
| 1283 |
case goog.i18n.NumberFormat.PATTERN_GROUPING_SEPARATOR_: |
| 1284 |
if (groupingCount > 0) { |
| 1285 |
this.groupingArray_.push(groupingCount); |
| 1286 |
} |
| 1287 |
groupingCount = 0; |
| 1288 |
break; |
| 1289 |
case goog.i18n.NumberFormat.PATTERN_DECIMAL_SEPARATOR_: |
| 1290 |
if (decimalPos >= 0) { |
| 1291 |
throw new Error( |
| 1292 |
'Multiple decimal separators in pattern "' + pattern + '"'); |
| 1293 |
} |
| 1294 |
decimalPos = digitLeftCount + zeroDigitCount + digitRightCount; |
| 1295 |
break; |
| 1296 |
case goog.i18n.NumberFormat.PATTERN_EXPONENT_: |
| 1297 |
if (this.useExponentialNotation_) { |
| 1298 |
throw new Error( |
| 1299 |
'Multiple exponential symbols in pattern "' + pattern + '"'); |
| 1300 |
} |
| 1301 |
this.useExponentialNotation_ = true; |
| 1302 |
this.minExponentDigits_ = 0; |
| 1303 |
|
| 1304 |
// exponent pattern can have a optional '+'. |
| 1305 |
if ((pos[0] + 1) < len && |
| 1306 |
pattern.charAt(pos[0] + 1) == |
| 1307 |
goog.i18n.NumberFormat.PATTERN_PLUS_) { |
| 1308 |
pos[0]++; |
| 1309 |
this.useSignForPositiveExponent_ = true; |
| 1310 |
} |
| 1311 |
|
| 1312 |
// Use lookahead to parse out the exponential part |
| 1313 |
// of the pattern, then jump into phase 2. |
| 1314 |
while ((pos[0] + 1) < len && |
| 1315 |
pattern.charAt(pos[0] + 1) == |
| 1316 |
goog.i18n.NumberFormat.PATTERN_ZERO_DIGIT_) { |
| 1317 |
pos[0]++; |
| 1318 |
this.minExponentDigits_++; |
| 1319 |
} |
| 1320 |
|
| 1321 |
if ((digitLeftCount + zeroDigitCount) < 1 || |
| 1322 |
this.minExponentDigits_ < 1) { |
| 1323 |
throw new Error('Malformed exponential pattern "' + pattern + '"'); |
| 1324 |
} |
| 1325 |
loop = false; |
| 1326 |
break; |
| 1327 |
default: |
| 1328 |
pos[0]--; |
| 1329 |
loop = false; |
| 1330 |
break; |
| 1331 |
} |
| 1332 |
} |
| 1333 |
|
| 1334 |
if (zeroDigitCount == 0 && digitLeftCount > 0 && decimalPos >= 0) { |
| 1335 |
// Handle '###.###' and '###.' and '.###' |
| 1336 |
var n = decimalPos; |
| 1337 |
if (n == 0) { // Handle '.###' |
| 1338 |
n++; |
| 1339 |
} |
| 1340 |
digitRightCount = digitLeftCount - n; |
| 1341 |
digitLeftCount = n - 1; |
| 1342 |
zeroDigitCount = 1; |
| 1343 |
} |
| 1344 |
|
| 1345 |
// Do syntax checking on the digits. |
| 1346 |
if (decimalPos < 0 && digitRightCount > 0 || |
| 1347 |
decimalPos >= 0 && (decimalPos < digitLeftCount || |
| 1348 |
decimalPos > digitLeftCount + zeroDigitCount) || |
| 1349 |
groupingCount == 0) { |
| 1350 |
throw new Error('Malformed pattern "' + pattern + '"'); |
| 1351 |
} |
| 1352 |
var totalDigits = digitLeftCount + zeroDigitCount + digitRightCount; |
| 1353 |
|
| 1354 |
this.maximumFractionDigits_ = decimalPos >= 0 ? totalDigits - decimalPos : 0; |
| 1355 |
if (decimalPos >= 0) { |
| 1356 |
this.minimumFractionDigits_ = digitLeftCount + zeroDigitCount - decimalPos; |
| 1357 |
if (this.minimumFractionDigits_ < 0) { |
| 1358 |
this.minimumFractionDigits_ = 0; |
| 1359 |
} |
| 1360 |
} |
| 1361 |
|
| 1362 |
// The effectiveDecimalPos is the position the decimal is at or would be at |
| 1363 |
// if there is no decimal. Note that if decimalPos<0, then digitTotalCount == |
| 1364 |
// digitLeftCount + zeroDigitCount. |
| 1365 |
var effectiveDecimalPos = decimalPos >= 0 ? decimalPos : totalDigits; |
| 1366 |
this.minimumIntegerDigits_ = effectiveDecimalPos - digitLeftCount; |
| 1367 |
if (this.useExponentialNotation_) { |
| 1368 |
this.maximumIntegerDigits_ = digitLeftCount + this.minimumIntegerDigits_; |
| 1369 |
|
| 1370 |
// in exponential display, we need to at least show something. |
| 1371 |
if (this.maximumFractionDigits_ == 0 && this.minimumIntegerDigits_ == 0) { |
| 1372 |
this.minimumIntegerDigits_ = 1; |
| 1373 |
} |
| 1374 |
} |
| 1375 |
|
| 1376 |
// Add another number grouping at the end |
| 1377 |
this.groupingArray_.push(Math.max(0, groupingCount)); |
| 1378 |
this.decimalSeparatorAlwaysShown_ = |
| 1379 |
decimalPos == 0 || decimalPos == totalDigits; |
| 1380 |
}; |
| 1381 |
|
| 1382 |
|
| 1383 |
/** |
| 1384 |
* Alias for the compact format 'unit' object. |
| 1385 |
* @typedef {{ |
| 1386 |
* prefix: string, |
| 1387 |
* suffix: string, |
| 1388 |
* divisorBase: number |
| 1389 |
* }} |
| 1390 |
*/ |
| 1391 |
goog.i18n.NumberFormat.CompactNumberUnit; |
| 1392 |
|
| 1393 |
|
| 1394 |
/** |
| 1395 |
* The empty unit, corresponding to a base of 0. |
| 1396 |
* @private {!goog.i18n.NumberFormat.CompactNumberUnit} |
| 1397 |
*/ |
| 1398 |
goog.i18n.NumberFormat.NULL_UNIT_ = { |
| 1399 |
prefix: '', |
| 1400 |
suffix: '', |
| 1401 |
divisorBase: 0 |
| 1402 |
}; |
| 1403 |
|
| 1404 |
|
| 1405 |
/** |
| 1406 |
* Get compact unit for a certain number of digits |
| 1407 |
* |
| 1408 |
* @param {number} base The number of digits to get the unit for. |
| 1409 |
* @param {string} plurality The plurality of the number. |
| 1410 |
* @return {!goog.i18n.NumberFormat.CompactNumberUnit} The compact unit. |
| 1411 |
* @private |
| 1412 |
*/ |
| 1413 |
goog.i18n.NumberFormat.prototype.getUnitFor_ = function(base, plurality) { |
| 1414 |
var table = this.compactStyle_ == goog.i18n.NumberFormat.CompactStyle.SHORT ? |
| 1415 |
goog.i18n.CompactNumberFormatSymbols.COMPACT_DECIMAL_SHORT_PATTERN : |
| 1416 |
goog.i18n.CompactNumberFormatSymbols.COMPACT_DECIMAL_LONG_PATTERN; |
| 1417 |
|
| 1418 |
if (!goog.isDefAndNotNull(table)) { |
| 1419 |
table = goog.i18n.CompactNumberFormatSymbols.COMPACT_DECIMAL_SHORT_PATTERN; |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (base < 3) { |
| 1423 |
return goog.i18n.NumberFormat.NULL_UNIT_; |
| 1424 |
} else { |
| 1425 |
base = Math.min(14, base); |
| 1426 |
var patterns = table[Math.pow(10, base)]; |
| 1427 |
var previousNonNullBase = base - 1; |
| 1428 |
while (!patterns && previousNonNullBase >= 3) { |
| 1429 |
patterns = table[Math.pow(10, previousNonNullBase)]; |
| 1430 |
previousNonNullBase--; |
| 1431 |
} |
| 1432 |
if (!patterns) { |
| 1433 |
return goog.i18n.NumberFormat.NULL_UNIT_; |
| 1434 |
} |
| 1435 |
|
| 1436 |
var pattern = patterns[plurality]; |
| 1437 |
if (!pattern || pattern == '0') { |
| 1438 |
return goog.i18n.NumberFormat.NULL_UNIT_; |
| 1439 |
} |
| 1440 |
|
| 1441 |
var parts = /([^0]*)(0+)(.*)/.exec(pattern); |
| 1442 |
if (!parts) { |
| 1443 |
return goog.i18n.NumberFormat.NULL_UNIT_; |
| 1444 |
} |
| 1445 |
|
| 1446 |
return { |
| 1447 |
prefix: parts[1], |
| 1448 |
suffix: parts[3], |
| 1449 |
divisorBase: (previousNonNullBase + 1) - (parts[2].length - 1) |
| 1450 |
}; |
| 1451 |
} |
| 1452 |
}; |
| 1453 |
|
| 1454 |
|
| 1455 |
/** |
| 1456 |
* Get the compact unit divisor, accounting for rounding of the quantity. |
| 1457 |
* |
| 1458 |
* @param {number} formattingNumber The number to base the formatting on. The |
| 1459 |
* unit will be calculated from this number. |
| 1460 |
* @param {number} pluralityNumber The number to use for calculating the |
| 1461 |
* plurality. |
| 1462 |
* @return {!goog.i18n.NumberFormat.CompactNumberUnit} The unit after rounding. |
| 1463 |
* @private |
| 1464 |
*/ |
| 1465 |
goog.i18n.NumberFormat.prototype.getUnitAfterRounding_ = function( |
| 1466 |
formattingNumber, pluralityNumber) { |
| 1467 |
if (this.compactStyle_ == goog.i18n.NumberFormat.CompactStyle.NONE) { |
| 1468 |
return goog.i18n.NumberFormat.NULL_UNIT_; |
| 1469 |
} |
| 1470 |
|
| 1471 |
formattingNumber = Math.abs(formattingNumber); |
| 1472 |
pluralityNumber = Math.abs(pluralityNumber); |
| 1473 |
|
| 1474 |
var initialPlurality = this.pluralForm_(formattingNumber); |
| 1475 |
// Compute the exponent from the formattingNumber, to compute the unit. |
| 1476 |
var base = formattingNumber <= 1 ? 0 : this.intLog10_(formattingNumber); |
| 1477 |
var initialDivisor = this.getUnitFor_(base, initialPlurality).divisorBase; |
| 1478 |
// Round both numbers based on the unit used. |
| 1479 |
var pluralityAttempt = pluralityNumber / Math.pow(10, initialDivisor); |
| 1480 |
var pluralityRounded = this.roundNumber_(pluralityAttempt); |
| 1481 |
var formattingAttempt = formattingNumber / Math.pow(10, initialDivisor); |
| 1482 |
var formattingRounded = this.roundNumber_(formattingAttempt); |
| 1483 |
// Compute the plurality of the pluralityNumber when formatted using the name |
| 1484 |
// units as the formattingNumber. |
| 1485 |
var finalPlurality = |
| 1486 |
this.pluralForm_(pluralityRounded.intValue + pluralityRounded.fracValue); |
| 1487 |
// Get the final unit, using the rounded formatting number to get the correct |
| 1488 |
// unit, and the plurality computed from the pluralityNumber. |
| 1489 |
return this.getUnitFor_( |
| 1490 |
initialDivisor + this.intLog10_(formattingRounded.intValue), |
| 1491 |
finalPlurality); |
| 1492 |
}; |
| 1493 |
|
| 1494 |
|
| 1495 |
/** |
| 1496 |
* Get the integer base 10 logarithm of a number. |
| 1497 |
* |
| 1498 |
* @param {number} number The number to log. |
| 1499 |
* @return {number} The lowest integer n such that 10^n >= number. |
| 1500 |
* @private |
| 1501 |
*/ |
| 1502 |
goog.i18n.NumberFormat.prototype.intLog10_ = function(number) { |
| 1503 |
// Handle infinity. |
| 1504 |
if (!isFinite(number)) { |
| 1505 |
return number > 0 ? number : 0; |
| 1506 |
} |
| 1507 |
// Turns out Math.log(1000000)/Math.LN10 is strictly less than 6. |
| 1508 |
var i = 0; |
| 1509 |
while ((number /= 10) >= 1) i++; |
| 1510 |
return i; |
| 1511 |
}; |
| 1512 |
|
| 1513 |
|
| 1514 |
/** |
| 1515 |
* Round to a certain number of significant digits. |
| 1516 |
* |
| 1517 |
* @param {number} number The number to round. |
| 1518 |
* @param {number} significantDigits The number of significant digits |
| 1519 |
* to round to. |
| 1520 |
* @param {number} scale Treat number as fixed point times 10^scale. |
| 1521 |
* @return {number} The rounded number. |
| 1522 |
* @private |
| 1523 |
*/ |
| 1524 |
goog.i18n.NumberFormat.prototype.roundToSignificantDigits_ = function( |
| 1525 |
number, significantDigits, scale) { |
| 1526 |
if (!number) return number; |
| 1527 |
|
| 1528 |
var digits = this.intLog10_(number); |
| 1529 |
var magnitude = significantDigits - digits - 1; |
| 1530 |
|
| 1531 |
// Only round fraction, not (potentially shifted) integers. |
| 1532 |
if (magnitude < -scale) { |
| 1533 |
var point = Math.pow(10, scale); |
| 1534 |
return Math.round(number / point) * point; |
| 1535 |
} |
| 1536 |
|
| 1537 |
var power = Math.pow(10, magnitude); |
| 1538 |
var shifted = Math.round(number * power); |
| 1539 |
return shifted / power; |
| 1540 |
}; |
| 1541 |
|
| 1542 |
|
| 1543 |
/** |
| 1544 |
* Get the plural form of a number. |
| 1545 |
* @param {number} quantity The quantity to find plurality of. |
| 1546 |
* @return {string} One of 'zero', 'one', 'two', 'few', 'many', 'other'. |
| 1547 |
* @private |
| 1548 |
*/ |
| 1549 |
goog.i18n.NumberFormat.prototype.pluralForm_ = function(quantity) { |
| 1550 |
/* TODO: Implement */ |
| 1551 |
return 'other'; |
| 1552 |
}; |
| 1553 |
|
| 1554 |
|
| 1555 |
/** |
| 1556 |
* Checks if the currency symbol comes before the value ($12) or after (12$) |
| 1557 |
* Handy for applications that need to have separate UI fields for the currency |
| 1558 |
* value and symbol, especially for input: Price: [USD] [123.45] |
| 1559 |
* The currency symbol might be a combo box, or a label. |
| 1560 |
* |
| 1561 |
* @return {boolean} true if currency is before value. |
| 1562 |
*/ |
| 1563 |
goog.i18n.NumberFormat.prototype.isCurrencyCodeBeforeValue = function() { |
| 1564 |
var posCurrSymbol = this.pattern_.indexOf('\u00A4'); // 'ยค' Currency sign |
| 1565 |
var posPound = this.pattern_.indexOf('#'); |
| 1566 |
var posZero = this.pattern_.indexOf('0'); |
| 1567 |
|
| 1568 |
// posCurrValue is the first '#' or '0' found. |
| 1569 |
// If none of them is found (not possible, but still), |
| 1570 |
// the result is true (postCurrSymbol < MAX_VALUE) |
| 1571 |
// That is OK, matches the en_US and ROOT locales. |
| 1572 |
var posCurrValue = Number.MAX_VALUE; |
| 1573 |
if (posPound >= 0 && posPound < posCurrValue) { |
| 1574 |
posCurrValue = posPound; |
| 1575 |
} |
| 1576 |
if (posZero >= 0 && posZero < posCurrValue) { |
| 1577 |
posCurrValue = posZero; |
| 1578 |
} |
| 1579 |
|
| 1580 |
// No need to test, it is guaranteed that both these symbols exist. |
| 1581 |
// If not, we have bigger problems than this. |
| 1582 |
return posCurrSymbol < posCurrValue; |
| 1583 |
}; |