There is a built-in function for this.

var a = 2;
var b = a.toFixed(1);

This rounds the number to one decimal place, and displays it with that one decimal place, even if it's zero.

Answer from Niet the Dark Absol on Stack Overflow
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ parseFloat
parseFloat() - JavaScript | MDN
The parseFloat function converts its first argument to a string, parses that string as a decimal number literal, then returns a number or NaN. The number syntax it accepts can be summarized as: The characters accepted by parseFloat() are plus sign (+), minus sign (- U+002D HYPHEN-MINUS), decimal ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ javascript-program-to-convert-float-to-string
JavaScript Program to Convert Float to String - GeeksforGeeks
August 5, 2025 - The String() function is one of the most commonly used and simplest ways to convert a float to a string. It works by converting the given value into its string representation. ... String(num) converts the float num into a string.
๐ŸŒ
W3Schools
w3schools.com โ€บ jsref โ€บ jsref_parsefloat.asp
JavaScript parseFloat() Method
The parseFloat() method parses a value as a string and returns the first number. If the first character cannot be converted, NaN is returned. Leading and trailing spaces are ignored.
๐ŸŒ
HScripts
hscripts.com โ€บ tutorials โ€บ javascript โ€บ string-float.php
Javascript (JS) Tutorial - type convert string to float, integer to string value
d) Converting Integer/Float to String Integer (int) or float value can be converted to string by using the function or method toString(). ... <script language="javascript"> var a = 32; var b = 333; var c = a.toString()+b; document.write(" to String function "+c); </script>
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Number โ€บ parseFloat
Number.parseFloat() - JavaScript | MDN
The value to parse, coerced to a string. Leading whitespace in this argument is ignored. A floating point number parsed from the given string. Or NaN when the first non-whitespace character cannot be converted to a number.
๐ŸŒ
Python Examples
pythonexamples.org โ€บ javascript โ€บ how-to-convert-float-to-string
How to convert Float to String in JavaScript
To convert a float to a string in JavaScript, you can use the toString() method of the Number object.
Find elsewhere
๐ŸŒ
EDUCBA
educba.com โ€บ home โ€บ software development โ€บ software development tutorials โ€บ javascript tutorial โ€บ javascript string to float
JavaScript String to Float | Two Methods to Convert String to Float
March 27, 2023 - Home Software Development Software Development Tutorials JavaScript Tutorial JavaScript String to Float ... Conversion of String to Float is generally used if the string including float numbers is to perform mathematical operations. When you get text field or text field data, you must receive data entered as a string. Let assume if the data we have in float and we want to convert in the string or we have data in the string and want to convert in float so we have two methods.
Call ย  +917738666252
Address ย  Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
๐ŸŒ
Linux Hint
linuxhint.com โ€บ convert-string-to-float-javascript
How to convert string to float in JavaScript
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
Favtutor
favtutor.com โ€บ articles โ€บ convert-string-to-float-javascript
Convert String to Float in JavaScript (5 Easy Ways)
November 30, 2023 - The next approach to convert a string to a float number is by subtracting 0 from the string. JavaScript automatically performs a type conversion while subtracting 0 from the string, converting a string to a number.
๐ŸŒ
Favtutor
favtutor.com โ€บ articles โ€บ number-to-string-javascript
Convert Number to String in JavaScript (5 Easy Methods)
November 23, 2023 - We have used the toFixed() method to convert a floating point number to a string.
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ parsefloat-javascript
parseFloat JavaScript: Syntax and Examples - Flexiple
This function returns a floating-point number parsed up to that point where it encounters a character that is not a number. ... String: This parameter, a mandatory input contains a string that is converted to a floating-point number.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnjavascript โ€บ library/function that will tidy up weird js float values without converting to string in between?
r/learnjavascript on Reddit: Library/function that will tidy up weird JS float values without converting to string in between?
July 13, 2024 -

Edit: I got the answer I needed below (multiplying by a power of ten, rounding, and then dividing again by the same power of ten appears to do the trick)


Is there any library out there that will a clean up floats (the results of a prior sequence of calculations)? For example:

1.24999999993 -> 1.25

2.99999999998 -> 3

-0.37499999982 -> -0.375 etc

I realize I can do +float.toFixed(precision), where precision is something like 6-9 to get a decent result, but Iโ€™m wondering if there is something out there that is more systematic and doesnโ€™t rely on strings.

I tried decimal.js and itโ€™s a mess because it would require me having to rewrite everything to handle its Decimal class, and in the end all of its presentation methods just convert the float values to strings anyway, so Iโ€™m better off just doing toFixed() and coercing back to a number instead of bringing in a whole library to use one method that does the same but only goes halfway.

Basically Iโ€™m just looking for a lightweight library or function someone has written that cleans up floats as numbers. Or is it just not possible without using intermediate strings in some way?

I tried googling for this already obviously, without any luck. Just lots of the same article and question about floating point inconsistencies in JS and other languages.

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-convert-a-string-to-a-floating-point-number-in-javascript
How to convert a string to a floating point number in JavaScript?
In the example below we are performing parseFloat() function on input string which contains spaces as first character. This function will ignore the spaces and returns the output as floating point number.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ how-to-convert-string-into-float-in-javascript
How to convert string into float in JavaScript? | GeeksforGeeks
June 21, 2024 - In this method, we will use the parseFloat() method which is an inbuilt function in JavaScript that is used to accept the string and convert it into a floating point number.
๐ŸŒ
Reddit
reddit.com โ€บ r/javascript โ€บ converting a string to a float in javascript?
r/javascript on Reddit: Converting a string to a float in Javascript?
March 27, 2015 -

I can't use parseFloat(). I am so far doing the obvious by checking each character, but I feel there has to be a more elegant way! My function needs to act exactly the same as parseFloat() does. Can anyone help?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-parse-float-with-two-decimal-places-in-javascript
How to Parse Float with Two Decimal Places in JavaScript? - GeeksforGeeks
The custom parseFloat method converts a number to a string, slices it to retain only the desired decimal places, and then converts it back to a number.
Published ย  July 23, 2025