HTML:
<div id="box">
<p> Valor: <input id="valor"> </p>
<p> Taxa: <input id="taxa"> </p>
<p> Imposto: <input id="imposto"> </p>
<p> Envio: <input id="envio"> </p>
<p> Taxa adicional: <input id="taxa_adicional"> </p>
<p> Subtotal: <span id="subtotal">0</span> </p>
<p> Total: <span id="total">0</span> </p>
</div>
JavaScript:
var options = {
format: '#,###.00',
locale: 'br'
},
inputs = $( 'input:text', '#box' ).get(),
input_adicional = $( '#taxa_adicional' )[0],
input_total = $( '#total' )[0],
input_subtotal = $( '#subtotal' )[0];
and then:
$( inputs ).add( [ input_total, input_subtotal ] ).format( options );
$( inputs ).
blur( function () {
var total = 0,
subtotal = 0;
// on blur, format the field
$( this ).format( options );
// calculate the sum of all input fields
$( inputs ).each( function () {
total += +$( this ).parse( options );
});
// subtotal doesn't include the "additional" field
subtotal = total - $( input_adicional ).parse( options );
// populate the SPAN's with the sums and format the nubmers
$( input_subtotal ).text( subtotal ).format( options );
$( input_total ).text( total ).format( options );
}).
focus( function () {
// if the field contains the value 0, empty it
if ( +$( this ).parse( options ) === 0 ) {
this.value = '';
}
});
Live demo: http://jsfiddle.net/U9V6x/
Answer from Šime Vidas on Stack OverflowW3Schools
w3schools.com › jsref › jsref_parsefloat.asp
JavaScript parseFloat() Method
The parseFloat() method parses a value as a string and returns the first number.
Top answer 1 of 4
7
HTML:
<div id="box">
<p> Valor: <input id="valor"> </p>
<p> Taxa: <input id="taxa"> </p>
<p> Imposto: <input id="imposto"> </p>
<p> Envio: <input id="envio"> </p>
<p> Taxa adicional: <input id="taxa_adicional"> </p>
<p> Subtotal: <span id="subtotal">0</span> </p>
<p> Total: <span id="total">0</span> </p>
</div>
JavaScript:
var options = {
format: '#,###.00',
locale: 'br'
},
inputs = $( 'input:text', '#box' ).get(),
input_adicional = $( '#taxa_adicional' )[0],
input_total = $( '#total' )[0],
input_subtotal = $( '#subtotal' )[0];
and then:
$( inputs ).add( [ input_total, input_subtotal ] ).format( options );
$( inputs ).
blur( function () {
var total = 0,
subtotal = 0;
// on blur, format the field
$( this ).format( options );
// calculate the sum of all input fields
$( inputs ).each( function () {
total += +$( this ).parse( options );
});
// subtotal doesn't include the "additional" field
subtotal = total - $( input_adicional ).parse( options );
// populate the SPAN's with the sums and format the nubmers
$( input_subtotal ).text( subtotal ).format( options );
$( input_total ).text( total ).format( options );
}).
focus( function () {
// if the field contains the value 0, empty it
if ( +$( this ).parse( options ) === 0 ) {
this.value = '';
}
});
Live demo: http://jsfiddle.net/U9V6x/
2 of 4
5
When you're calculating your subtotal, you are calling parseFloat() on a series of strings, not the variables themselves. The line should be:
var subtotal = parseFloat(val) + parseFloat(tax) + parseFloat(imp) + parseFloat(env);
Videos
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › parseFloat
parseFloat() - JavaScript | MDN
The parseFloat() function parses a string argument and returns a floating point number.
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Number › parseFloat
Number.parseFloat() - JavaScript | MDN
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
W3Schools
w3schools.com › jsref › jsref_number_parsefloat.asp
JavaScript Number.parseFloat()
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-number-parsefloat-method
JavaScript Number parseFloat() Method - GeeksforGeeks
July 11, 2025 - // It ignores leading and trailing spaces. a = parseFloat(" 100 ") console.log('parseFloat(" 100 ") = ' + a); // It returns floating point Number until // it encounters Not a Number character b = parseFloat("2018@geeksforgeeks") console.log('parseFloat("2018@geeksforgeeks") = ' + b); // It returns NaN on Non numeral character c = parseFloat("geeksforgeeks@2018") console.log('parseFloat("geeksforgeeks@2018") = ' + c); d = parseFloat("3.14") console.log('parseFloat("3.14") = ' + d); // It returns only first Number it encounters e = parseFloat("22 7 2018") console.log('parseFloat("22 7 2018") = ' + e);
Telerik
telerik.com › javascript › kendo › parsefloat
parseFloat - API Reference - Kendo UI kendo - Kendo UI for jQuery
*/ console.log(kendo.parseFloat("1.212,22 €")); // outputs "1212.22" /* The result can be observed in the DevTools(F12) console of the browser.
Codecademy
codecademy.com › docs › javascript › number methods › .parsefloat()
JavaScript | Number Methods | .parseFloat() | Codecademy
May 31, 2024 - In JavaScript, the .parseFloat() method parses a given string and returns the first floating-point number found in the string. Parsing stops when it encounters a character that is not part of a valid number.
W3Schools
www-db.deis.unibo.it › courses › TW › DOCS › w3schools › jsref › jsref_parsefloat.asp.html
JavaScript parseFloat() Function
The parseFloat() function parses a string and returns a floating point number.
JSFiddle
jsfiddle.net › vandalo › shnn7797
ParseFloat and cultures - JSFiddle - Code Playground
JSFiddle - Test your JavaScript, CSS, HTML or CoffeeScript online with JSFiddle.
Learning jQuery
learningjquery.com › home › jquery resources › jquery – convert string to float or double
jQuery – Convert string to float or double | Learning jQuery
January 8, 2021 - To convert, use JavaScript parseFloat() function parses a string and returns a floating point number. var sVal = '234.54'; var iNum = parseFloat(sVal); //Output will be 234.54. Related Post: jQuery- Convert string to Integer jQuery to round off decimal values jQuery Code: Change text
Javatpoint
javatpoint.com › javascript-number-parsefloat-method
JavaScript Number parseFloat() Method - javatpoint
JavaScript Number parseFloat() Method with example, javascript math methods, abs() method, round() method, ceil() method, floor() method, pow() method, random() method, sqrt() method, max() method, min() method, log() method, sin() method, cos() method, tan() method etc.
Programiz
programiz.com › javascript › library › built-in › parseFloat
JavaScript parseFloat()
// parse the string to float value let floatDate = parseFloat(stringDate); console.log(floatDate) // Output: 23.9
Learnjavascript
learnjavascript.co.uk › reference › globals › parsefloat.html
JavaScript parseFloat() Function
// Parsing string to floats. var parsedValues = new Array(6); parsedValues[0] = parseFloat('17'); // integer parsedValues[1] = parseFloat('17.2345'); // valid parsedValues[2] = parseFloat('-17.2345'); // negative parsedValues[3] = parseFloat(' 17.2345 '); // within spaces parsedValues[4] = parseFloat('17.2345ignored'); // ignore non decimal suffix parsedValues[5] = parseFloat('abcdef'); // invalid alert(parsedValues); JavaScript Intermediate Tutorials - Lesson 6 - More Maths Functions · << Object · parseInt() >> JavaScript & jQuery Tutorials
TutorialsPoint
tutorialspoint.com › how-to-parse-float-with-two-decimal-places-in-javascript
How to parse float with two decimal places in JavaScript?
To parse float with two decimal places, use the concept of toFixed(2). Following is the code − Example var price = 178932.89; var tax = 7.9; var totalPrice = price*tax; console.log("The ac
GeeksforGeeks
geeksforgeeks.org › how-to-parse-float-with-two-decimal-places-in-javascript
How to Parse Float with Two Decimal Places in JavaScript? | GeeksforGeeks
JavaScript provides an inbuilt parseFloat() method to parse a string and returns a floating-point number.
Published January 9, 2025