How can I use JQuery to add a decimal and trailing zeros to currency?

I have checked all related questions and none, fully address my question. I know how to convert to money with symbols and commas, but not sure how to add trailing zeros if the user did not specify cents.

I did come across a post that mentioned adding this .toFixed(2) but I do not understand how to incorporate that into my function.

Desired function is 14578 to convert to $14,578.00 but do not add the zeros if the number were 14578.79 to $14,578.79. Also I noticed that it puts a dollar sign in the field even if left blank. Any way to avoid that?

$('.money').blur(function(e){        
    $(this).val(formatCurrency(this.value.replace(/[,$]/g,'')));
}).on('keypress',function(e){
    if(!$.isNumeric(String.fromCharCode(e.which))) e.preventDefault();
}).on('paste', function(e){    
    var cb = e.originalEvent.clipboardData || window.clipboardData;      
    if(!$.isNumeric(cb.getData('text'))) e.preventDefault();
});
function formatCurrency(number){
    var n = number.split('').reverse().join("");
    var n2 = n.replace(/ddd(?!$)/g, "$&,");    
    return '$' + n2.split('').reverse().join('');
}
input {
  display: block;
}
https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js
<input type='text' class="money"/>
<input type='text'/>
<input type='text' class="money"/>
<input type='text'/>
<input type='text' class="money"/>
<input type='text'/>
<input type='text' class="money"/>
<input type='text'/>

Thank you!


Source: stackoverflow-javascript