-2

i get a float value by this code

currentvalue=$("#double_your_btc_stake").val();

but when i try to write double of the old value

currentvalue*=2;$("#double_your_btc_stake").val(currentvalue);

but it write to input box

2e-8

what i do wrong

http://jsfiddle.net/62jJM/

user2061745
  • 305
  • 2
  • 3
  • 12

2 Answers2

0

Try this out:- http://jsfiddle.net/adiioo7/LCmjG/1/

JS:-

jQuery(function ($) {
    $("#Double").on("click", function () {
        var currentvalue = $("#double_your_btc_stake").val();
        currentvalue *= 2;
        $("#double_your_btc_stake").val(longnumberstring(currentvalue));
    });
});

function longnumberstring(n) {
    var str, str2 = '',
        data = n.toExponential().replace('.', '').split(/e/i);
    str = data[0], mag = Number(data[1]);
    if (mag >= 0 && str.length > mag) {
        mag += 1;
        return str.substring(0, mag) + '.' + str.substring(mag);
    }
    if (mag < 0) {
        while (++mag) str2 += '0';
        return '0.' + str2 + str;
    }
    mag = (mag - str.length) + 1;
    while (mag > str2.length) {
        str2 += '0';
    }
    return str + str2;
}

HTML:-

<input type="text" id="double_your_btc_stake" style="width:500px" />
<input type="button" id="Double" value="Double" />

Used Reference to convert the exponential to long string.

Community
  • 1
  • 1
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
-2

Simply use this:

parseFloat($("#yourId").val())
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
  • 2
    This should not be necessary. The multiplication will implicitly convert the string to a number (if it's a valid number of course). – Pointy Nov 20 '13 at 14:34