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 ...
🌐
W3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript | MDN
The Number.parseFloat() static method parses an argument and returns a floating point number. If a number cannot be parsed from the argument, it returns NaN. function circumference(r) { if (Number.isNaN(Number.parseFloat(r))) { return 0; } return parseFloat(r) * 2.0 * Math.PI; } console.log(circumference("4.567abcdefgh")); // Expected output: 28.695307297889173 console.log(circumference("abcdefgh")); // Expected output: 0 ... The value to parse, coerced to a string.
🌐
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
🌐
Codecademy
codecademy.com › docs › javascript › number methods › .parsefloat()
JavaScript | Number Methods | .parseFloat() | Codecademy
May 31, 2024 - The following examples demonstrate how the .parseFloat() method is used to convert a value to a floating-point number: // Parses the number 3.14 as a floating-point number. ... // Parses the string ' 2.59 ' as a floating-point number after trimming ...
🌐
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.
🌐
ReScript
rescript-lang.org › docs › manual › latest › api › js › float
Js.Float | ReScript API
See toString on MDN. ... Formats a float as a string. radix specifies the radix base to use for the formatted number. The value must be in the range [2, 36] (inclusive). Return a string representing the given value in fixed-point (usually).
🌐
SmartBear Community
community.smartbear.com › smartbear community › testcomplete › testcomplete questions
Converting Float to String with decimal length | SmartBear Community
I have tried to convert the diff from Float to String but it showing as "5.000000000003E-02" Any help with decimal positions in Float or exact conversion of Float to Str ? I am using TestComplete Version: 14.73.382.7 x64 · Variables · Reply · rraghvani3 years ago · For example (JavaScript), function test() { var a = 2.85; var b = 2.8; var diff = a - b; Log.Message(diff.toFixed(2)); } Marked as Solution ·
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-string-into-float-in-javascript
How to convert string into float in 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> </body> </html>
🌐
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.
🌐
Vultr Docs
docs.vultr.com › javascript › global › parseFloat
JavaScript parseFloat() - Parse String to Float | Vultr Docs
November 6, 2024 - The parseFloat() function in JavaScript is crucial for converting a string representation of a number into a floating point number. This function is often employed when handling user inputs that require numerical calculations but are received ...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › toString
Number.prototype.toString() - JavaScript | MDN
Rather, the algorithm uses the least number of significant figures necessary to distinguish the output from adjacent number values. For example, if the number is large, there will be many equivalent string representations of the same floating point number, and toString() will choose the one with the most 0s to the right (for any given radix).
🌐
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.
🌐
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.

🌐
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.
🌐
GitHub
github.com › prisma › prisma › issues › 12471
`JSON.stringify` is converting `Float` (`number`) values to strings. · Issue #12471 · prisma/prisma
March 22, 2022 - Bug description I'm using prisma with Typescript in a next.js application and JSON.strinfigy converts fields that are of type number (Float in the schema) to strings. I did a little debugging and it looks like the Float fields are actual...
Published   Mar 22, 2022