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 method first tries to read the [input] as integer (similar to |
| 367 * [int.parse] without a radix). |
| 368 * If that fails, it tries to parse the [input] as a double (similar to |
| 369 * [double.parse]). |
| 370 * If that fails, too, it invokes [onError] with [input]. |
| 371 * |
| 372 * If no [onError] is supplied, it defaults to a function that throws a |
| 373 * [FormatException]. |
| 374 * |
| 375 * For any number `n`, this function satisfies |
| 376 * `identical(n, num.parse(n.toString()))`. |
| 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 |