Usman Khan
Usman Khan

Reputation: 179

values not caltulating properly

I have created function to devide the values but the problem is that when place function parseInt and check it is avoiding numbers after (,) like if the value is 345,00.95 after using parseInt() it is becoming as 645 can any one help me out why is this happening

  function liquid(val) {
    $(".overlay").show();
    $.ajax({
        url: "portfolio/getPortfolioInfo.php",
        dataType: "json",
        data: "portfolio_id=" + val,
        type: "post",
        error:function(){
            $(".overlay").hide();
        },
        success: function (data) {
            $(".overlay").hide();
            $("#liquidity").html(data.liquidity);
            var num_left = data.liquidity / parseFloat(<?php echo $company['price']; ?>);
            var rounded_value   = Math.round(num_left);
            $(".shares_left").html("<p>You can buy up to " + rounded_value + " </p>");
            console.log("liquidity" + parseInt(data.liquidity) + "rate" +parseFloat(<?php echo $company['price']; ?>));
        }
    });
}

Upvotes: 0

Views: 39

Answers (1)

Karthik Ganesan
Karthik Ganesan

Reputation: 4222

considering the sample you provided

parseInt(345,00.95)

will output only the initial integer part that being

345

in your case

even if you had removed the initial ',' that is if your number was parseInt(34500.95) that will still give out just 34500 ignoring the decimal part

to achieve what you are looking for first remove all the ',' from your data

to do that use replace with a regex that matches all ',' within your data

(data.liquidity).replace(/,/g,'')

this will output

34500.95

and then apply parseFloat to it

so your final code should be parseFloat((data.liquidity).replace(/,/g,''));

Upvotes: 1

Related Questions