pers0n
pers0n

Reputation: 155

if i store the value of input type number in another variable it comes missing a number at the end

so I'm making this calculator thing and I've come across an issue, when I store the value of input type number in another variable it comes missing a number like this picture:pic of the issue

so the getValue() function gets the fraction of a number or a string and here's its code:

function getValue(v) {
    return +v.toString().match(/\.(\d*)/)[1] || 0;
}

Anyway if I got this issue fixed I think I could finally finish this application, so any help would be appreciated and thanks in advance.

Edit: I forgot to add the code that stores the value of the input, so here it is:

input.addEventListener("keypress", function() {
    num = input.value * 1;  
    re1 = input.value;
});

Upvotes: 0

Views: 19

Answers (1)

Nguyễn Văn Phong
Nguyễn Văn Phong

Reputation: 14198

You should use keyup event instead of keypress

function getValue(v) {
    return +v.toString().match(/\.(\d*)/)[1] || 0;
}

$("input").on("keyup", function() {
    num = $(this).val() * 1;  
    re1 = $(this).val();
    console.log(re1);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input />

Upvotes: 1

Related Questions