← Back to JavaScript
19

Strings and numbers without surprises: parsing, formatting and precision

Learn how to convert and format user input and API values without accidental coercion or rounding mistakes, using a safer workflow for real frontend code.

📘 Theory

Input, conversion, validation and output

Do not mix responsibilities. Interpret first, calculate second and format last.

Think in four steps: receive raw input, convert it to the working type, validate that it is usable and only then format it for display.

Separating those phases helps you debug much faster when a total or formatted value looks wrong.

1

Input

Raw value with no trust yet.

  • `'19.99'` from an input
  • `'1,250.40'` from a legacy API
2

Conversion

Turn it into the type you really need.

  • `String(value)`
  • `Number(value)` or `parseFloat(cleaned)`
3

Validation

Check that the value is usable.

  • `Number.isFinite(number)`
  • Business range rules
4

Output

Present it in human format.

  • `Intl.NumberFormat` for currency
  • Template literals for UI text

Safe parsing and reliable formatting

Converting is not enough. You also need to clean and validate.

1

`Number('')` returns `0`, which can introduce false positives in forms. That is why empty strings should usually be handled before conversion.

2

For money and display values, calculate with raw numbers first and only format for the user at the end with `Intl.NumberFormat`.

Floating-point reality and rounding

Values such as `0.1 + 0.2` are not bugs in your app. They are part of how floating-point math works.

1

JavaScript uses binary floating-point arithmetic, so some decimals cannot be represented perfectly. That is why `0.1 + 0.2` may produce `0.30000000000000004`.

2

For money, one safe strategy is to work in cents as integers and only format at the end. For local rounding, `Number.EPSILON` can help stabilize `Math.round`.

🧪 Learn by doing

Example Guided example: checkout total from form data Convert price and quantity from strings, validate them and calculate subtotal, tax and total.
Example Interactive demo: order calculator with validation Enter price, quantity and discount. The demo cleans the data, calculates the result and warns you when the input is invalid.

🏁 Challenges

Challenge Challenge 1: a robust price parser Implement a function that converts text into a number and returns `null` when the value is not valid.

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com