locale semi-agnostic string to float conversion [JavaScript]
✓ Published0🌍 Public
GGerHobbelt
Last edited Sep 9, 2012
Created on Mar 14, 2012
This example demonstrates a JavaScript utility for converting strings to floating-point numbers, designed to be locale semi-agnostic. It shows how the function handles various numeric formats, including European decimal commas, thousands separators, and ambiguous cases like “1.000” or “2,999.95”, returning numeric values or `NaN` for invalid inputs. The code uses the `toLocaleString()` method to detect the current locale’s decimal and thousands separators, then applies heuristic rules to interpret strings based on separator placement and digit counts. A MooTools click handler triggers the conversion examples, logging results to the console, with an accompanying HTML page featuring a product price display.
AI-generated description