all. I'm trying to change a string into a float so that it can be formatted as currency. Problem is, parseFloat keeps returning NaN. What am I missing? Here is the JSFiddle .
HTML:
<div class="value num">948572</div>
<div class="value num">34567</div>
<div class="value num"></div>
JS:
jQuery(document).ready(function($) {
$('.value.num').each(function() {
if( $(this).is(':empty')) {
$(this).text('$0.00');
} else {
var numValue = Number.parseFloat($(this), 10);
$(this).text('$' + (numValue / 100).toFixed(2));
}
});
});
If you want to get the text of an html element using jQuery use .text()
jQuery(document).ready(function($) {
$('.value.num').each(function() {
if( $(this).is(':empty')) {
$(this).text('$0.00');
} else {
var numValue = Number.parseFloat($(this).text(), 10);
$(this).text('$' + (numValue / 100).toFixed(2));
}
});
});
$(this)
refers to the .value.num
element. Change it to:
var numValue = Number.parseFloat($(this).text());
(Also, there is no second parameter to parseFloat
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.