Skip to content

Instantly share code, notes, and snippets.

@karloscarweber
Last active December 17, 2015 03:38
Show Gist options
  • Save karloscarweber/5544502 to your computer and use it in GitHub Desktop.
Save karloscarweber/5544502 to your computer and use it in GitHub Desktop.
Adds a dot to indicate cents.
var intervalID = 0;
timer = 0;
// add's comma's to every third decimal place from the end
intervalID = window.setInterval(function(){
addDots(document.getElementById("bidAmount"))
}, 1);
$('#bidAmount').onKeyUp(delay());
function delay() {
timer = 100;
console.log(timer);
}
function addDots(inObj){
if(timer > 1){
timer = timer - 1;
console.log(timer);
} else if (timer == 1 ) {
outStr = inObj.value.replace(/\./g, "");
leftStr = outStr
rightStr = ""
if (outStr.indexOf(".") > -1){
leftStr = outStr.substr(0, outStr.indexOf("."))
rightStr = outStr.substr(outStr.indexOf(".")+1)
}
if (leftStr.length > 2){
numCommas = Math.floor(( leftStr.length - 1 ) / 2 );
tempStr = ""
// for (x=0; x < numCommas; x++){
tempStr = "." + leftStr.substr(leftStr.length-2) + tempStr
leftStr = leftStr.substr(0,leftStr.length-2)
// }
leftStr += tempStr
}
outStr = outStr.indexOf(".") > -1 ? leftStr + "." + rightStr : leftStr
inObj.value = outStr
} else {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment