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.
🌐
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.
🌐
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>
🌐
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.
Find elsewhere
🌐
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
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-string-into-float-in-javascript
How to convert string into float in JavaScript?
It's easy to use and is built into JavaScript. The syntax of the parseFloat() function is as follows ? ... <!DOCTYPE html> <html> <body> <p id="demo"></p> <script> let string = "3.14"; let float = parseFloat(string); document.getElementById("demo").innerHTML = float; //output:3.14 </script> ...
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-convert-string-into-float-in-javascript
How to convert string into float in JavaScript? - GeeksforGeeks
July 12, 2025 - 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.
🌐
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.
🌐
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.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
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.

🌐
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
🌐
W3Schools
w3schools.com › jsref › jsref_tostring_number.asp
JavaScript Number toString() Method
The toString() method is used by JavaScript when an object needs to be displayed as a text (like in HTML), or when an object needs to be used as a string.
🌐
TutorialKart
tutorialkart.com › javascript › javascript-convert-string-to-float
How to convert String to Float in JavaScript?
September 23, 2021 - Any spaces around the string will be implicitly trimmed by the parseFloat() function. ... <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> </head> <body> <div id="output"></div> <script> var str = ' 3.14 '; var x = parseFloat(str); ...