Float Round

by Dmytro Tsiniavskyi

HTML

<input type="text" value="0.5" id="1" /><br/>
<input type="text" value="0.12" id="2" /><br/>
<input type="text" value="1" id="3" /><br/>
<input type="text" value="2.9" id="4" /><br/>
<input type="text" value="3.5" id="5" /><br/>

JavaScript

$(document).ready(function(){
    console.clear();
    $('input').each(function(){
        var f = parseFloat($(this).val());
        var r = round2(f);
        console.log(f + ' ' + r)
    });
});

function round1(number) {
    var decimals = number % 1 > 0 ? 2 : 0;
    return Number(Math.round(number+'e'+decimals)+'e-'+decimals);
}

function round2(number) {
    var decimals = number % 1 > 0 ? 2 : 0;
    return number.toFixed(decimals);
}