views:

48

answers:

1

Hello,

My Html is like this:

<input class="gbTransform" type="text" name="maxdl" value=""/>

And javascript like this:

$('.gbTransform').change(function(){
        var sz = $(this).val().match(/(\d+)gb/i);
        if(sz.length > 1){
          $(this).val(parseInt(sz[1])*1024);
        }
    });

What this does is when a user types 1gb it will automatically calculate into mbs (1 * 1024 = 1024) and change the input fields value.

This works fine but when user types 1.5gb its calculates 5 * 1024 which is incorrect it should be 1.5 * 1024

Thank You.

A: 

You can clear all "gb" strings and use parseFloat:

parseFloat("1.5"); // 1.5

Example:

var a = "1.5gb".replace("gb", "");
document.print(parseFloat(a));
Darmen
Still I am getting an incorrect answer I change parseInt to parseFloat but when I type `1.5gb` the value of field is calculated as `5 * 1024 = 5120` instead of `1.5 * 1024 = 1536`
Shishant
please look at updated answer
Darmen