Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * An integer or floating-point number. | 8 * An integer or floating-point number. |
| 9 * | 9 * |
| 10 * It is a compile-time error for any type other than [int] or [double] | 10 * It is a compile-time error for any type other than [int] or [double] |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 * 12.345e67.toString(); // 1.2345e+68 | 352 * 12.345e67.toString(); // 1.2345e+68 |
| 353 * | 353 * |
| 354 * Note: the conversion may round the output if the returned string | 354 * Note: the conversion may round the output if the returned string |
| 355 * is accurate enough to uniquely identify the input-number. | 355 * is accurate enough to uniquely identify the input-number. |
| 356 * For example the most precise representation of the [double] `9e59` equals | 356 * For example the most precise representation of the [double] `9e59` equals |
| 357 * `"899999999999999918767229449717619953810131273674690656206848"`, but | 357 * `"899999999999999918767229449717619953810131273674690656206848"`, but |
| 358 * this method returns the shorter (but still uniquely identifying) `"9e59"`. | 358 * this method returns the shorter (but still uniquely identifying) `"9e59"`. |
| 359 * | 359 * |
| 360 */ | 360 */ |
| 361 String toString(); | 361 String toString(); |
| 362 | |
| 363 /** | |
| 364 * Parses a string containing a number literal into a number. | |
| 365 * | |
| 366 * The input string should be either one accepted by [int.parse] (with no | |
|
floitsch
2013/11/25 12:50:53
won't work in dartdoc: []()
Not sure if much bett
Lasse Reichstein Nielsen
2013/11/25 13:05:16
That is better. Done.
| |
| 367 * radix) or by [double.parse]. | |
| 368 * If it would be accepted by [int.parse], | |
| 369 * the result is the integer that `int.parse` would return. | |
| 370 * Otherwise the result is the same result that `double.parse` would return. | |
| 371 * | |
| 372 * If the input is acceptable by neither `int.parse` nor `double.parse`, | |
| 373 * the result is found by calling [onError] with the input. | |
| 374 * If no [onError] is suppled, a [FormatException] is thrown. | |
|
floitsch
2013/11/25 12:50:53
supplied
Lasse Reichstein Nielsen
2013/11/25 13:05:16
Replaced by your wording above.
| |
| 375 * | |
| 376 * For any number `n`, this function satisfies `n == num.parse(n.toString())`. | |
|
floitsch
2013/11/25 12:50:53
any number `n`, not NaN, ...
or use identical(n,
Lasse Reichstein Nielsen
2013/11/25 13:05:16
Used identical. That's what the test tests too.
| |
| 377 */ | |
| 378 static num parse(String input, [num onError(String input)]) { | |
| 379 String source = input.trim(); | |
| 380 // TODO(lrn): Optimize to detect format and result type in one check. | |
| 381 num result = int.parse(source, onError: _returnNull); | |
| 382 if (result != null) return result; | |
| 383 result = double.parse(source, _returnNull); | |
| 384 if (result != null) return result; | |
| 385 if (onError == null) throw new FormatException(input); | |
| 386 return onError(input); | |
| 387 } | |
| 388 | |
| 389 /** Helper function for [parse]. */ | |
| 390 static _returnNull(_) => null; | |
| 362 } | 391 } |
| OLD | NEW |