dddexxx
dddexxx

Reputation: 31

Converting string to int/float in jQuery

I am getting a value from a div :

<div id="test">100</div>

And what I want to do is to get the value with jQuery, raise with 50% and show the value.

$number= $('#test').text() //getting the value, without parseInt or parseFloat?
$newNumber = $number * 0,5 //show the result of this

I often end up with NaN, so I'd like to have a guidance on that. Thank you.

Upvotes: 0

Views: 4105

Answers (2)

Gautam G
Gautam G

Reputation: 494

Suppose we have following markups:

<div id="test">100</div>
<div id="result"></div>

what you are looking for can be achieved as following (without using parseInt() or parseFloat() functions)

var number = $('#test').text();
var result = Number(number) + (number * 0.5);
$('#result').html(result);

Upvotes: 2

A. Wolff
A. Wolff

Reputation: 74420

You have error in your code, just fix it with:

$number * 0.5

Using dot, not comma.

Upvotes: 2

Related Questions