Note Validates US-formatted numbers with optional commas every 3 digits and optional decimal part. European formats swap comma and period -- adjust the pattern accordingly. The leading -? handles negative numbers.
Frequently asked questions
How do you format number?
This task is covered in 3 stacks on this page: Python, JavaScript, Regular Expressions. The "f-strings (Formatted String Literals)" snippet in Python uses `f"text {expression}"`.
Which code does the Python example use?
The "f-strings (Formatted String Literals)" snippet uses `f"text {expression}"`, from the Strings section of the Python cheat sheet.
Which stacks cover "format number" on this page?
Python, JavaScript, Regular Expressions. Together they hold 4 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "f-strings (Formatted String Literals)": Since Python 3.12, f-strings can contain backslashes and nested quotes freely. You can also nest f-strings inside f-strings.