Number Parser - Problem
Parse strings to numerical values (integers and floats) with comprehensive error handling. Your solution should support various input formats including:
- Standard numbers:
"123","-45","3.14" - Formatted numbers:
"1,000","1,234.56" - Currency format:
"$5.99","$1,234" - Scientific notation:
"1e3","2.5e-2"
Return the parsed number as a float. If the string cannot be parsed, return null.
Important: Handle edge cases like empty strings, invalid characters, and malformed numbers gracefully.
Input & Output
Example 1 — Currency Format
$
Input:
s = "$1,234.56"
›
Output:
1234.56
💡 Note:
Remove the dollar sign and comma, then parse '1234.56' to get 1234.56
Example 2 — Scientific Notation
$
Input:
s = "1e3"
›
Output:
1000.0
💡 Note:
Scientific notation: 1 × 10³ = 1000.0
Example 3 — Invalid Input
$
Input:
s = "abc123"
›
Output:
null
💡 Note:
Contains invalid characters 'abc' before the number, cannot be parsed
Constraints
- 1 ≤ s.length ≤ 100
- s contains only digits, letters, and symbols: $ , . + - e E
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code