Reputation: 863
I have a keypad of which inputs numbers for entering a payment.
With this i have leading zeros and would like to know the best way forward to dealing with/removing them efficiently.
The JS/JQ
$(".button_epos_e").click( function() {
btn_id = $(this).attr("id");
btn_num = btn_id.replace("pay_", "");
// alert(btn_num);
decimal = $("#number").text();
// alert(decimal);
var truenumber = decimal.replace(".", "");
// alert(truenumber);
truenumber += btn_num;
//alert(truenumber);
postDecimal = truenumber.slice(-2);
// alert(postDecimal);
preDecimal = truenumber.replace(postDecimal, "");
preDecInt = parseInt(preDecimal);
// alert(preDecimal);
newDecimal = preDecimal+"."+postDecimal;
$("#number").html(newDecimal);
});
Upvotes: 0
Views: 546
Reputation: 22637
There's quite a bit of things that should be changed. But this should suffice for your needs:
preDecimal = +truenumber.slice(0, -2) + "";
The +truenumber.slice(0, -2)
part converts the string to a number, deleting unnecessary leading zeros, and adding an empty string converts it back to a string (which isn't really necessary, though).
Upvotes: 1