Number rounding?

Hi all, having some problems trying to find a solution to this - the code bleow gives a return, of for example, 2 digits before the point and two after, but am trying to get the results to give any amount of numbers before the decimal point, and 3 digits after it, rounded to the nearest whole number, so so that if the answer, for example, should be, 123.655483456 it would give an answer of 123.656 - currently have :

function convertee(){
    value = document.inputFormee.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value/0.568261);
    arg = (parseInt(arg*100)/100).toFixed(2);
    document.inputFormee.result.value = arg;
}

Have looked around here and Googled it, and have found loads of examples of Math.round But they don’t tell you how to combine the both :wink:

Any help much appreciated.

Dez.


Math.round(arg*1000)/1000

No .toFixed() needed.

To give an example on how this works:

arg = 123.655483456;
arg * 1000 = 123655.483456;
Math.round(arg * 1000) = 123655
Math.round(arg * 1000) / 1000 = 123.655

And yes, it is 123.655 and not 123.656, you rounded wrong in your post :slight_smile:

Thanks for the quick help Scallio - that’s appreciated, will give that a try.

I boobed on the rounding :injured: If it had of been 123.655563456 would it then have rounded to 123.656 ?

Just realised, do you know the complete code, without any mention of any numbers please ? I was only using those numbers like 123.655483456 as examples of what users might input into forms.

The helps appreciated.

Everything smaller than .5 is rounded down, so 1.49 = 1, 2.49 = 2, etc
Everything greater than or equal to .5 is rounded up, so 1.50 = 2, 2.50 = 3, etc.

As for the whole code:


function convertee(){
    value = document.inputFormee.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value/0.568261);
    arg = Math.round(arg*1000)/1000;
    document.inputFormee.result.value = arg;
}

Or, if you want reusable code


function roundToDigits(number,digits) {
  var p = Math.pow(10, digits);
  return Math.round(number*p)/p;
}

And then


function convertee(){
    value = document.inputFormee.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value/0.568261);
    arg = roundToDigits(arg, 3);
    document.inputFormee.result.value = arg;
}

The toFixed(2) on the end will automatically round to two decimal places without any need to mess around with multiplying dividing and using Math.round().

toFixed() rounds up, i.e., used Math.ceil(), whereas Math.round() does “true” rounding (as far as “true” rounding is defined anyway :))

Thanks - would that always give 3 digits after the decimal point ?

Not always, depends on the number of digits after the decimal points in the input


Number of digits in input | Number of digits in output
------------------------------------------------------
                3 or more | 3
                        2 | 2
                        1 | 1
                        0 | 0

Thanks, now I see where I’m going wrong, it isn’t actually rounding that I’m looking for, it’s this, for example, using :

function convertpl(){ 
    value = document.inputFormpl.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value*0.56826125);
    arg = (parseInt(arg*100)/100).toFixed(3);
    document.inputFormpl.result.value = arg;
}

Inputting 25 times that figure of 0.56826125, and fixing at 3 digits after the decimal point, it reads 14.200, but should read 14.206 - how do I get that to happen please, just for it to cut off after the 3rd digit after the decimal point ? ? (Which I thought I was doing in the above code ;-(

The helps appreciated.

Dez.

May I ask what was wrong with my code?
Is it just that you always want three decimals, or is it something else?

No, nothing wrong with your code, it’s just that after using it in real-life examples, I could then see that no rounding at all would be better, and to just cut off after the 3rd digit after the decimal point.

Try


function convertee(){
    value = document.inputFormee.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value/0.568261);
    arg = arg.toPrecision(String(arg.toFixed(0)).length + 3);
    document.inputFormee.result.value = arg;
}

I’m sure there are better solutions, but this works.

Thanks ScallioXTX, it’s appreciated, but somethings still not quite right. Using the exact figures in the arg below, as these are the complete figures, say for example that the value was 25, so 25/0.56826125 = 43.993853, so cutting off after the 1st 3 digits after the decimal point, should be 43.993, but it isn’t, it’s coming out as 43.994 ? I know it’s only a slight difference, but for it to be precise, what code is there, that will just cut off after the 1st 3 digits after the decimal point and not round at all ? ?

function convertee(){
    value = document.inputFormee.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value/0.56826125);
    arg = arg.toPrecision(String(arg.toFixed(0)).length + 3);
    document.inputFormee.result.value = arg;
}

Anyone ?

parseInt(arg*100)/100) truncates to two decimal places and so the toFixed(3) following doesn’t have the extra digits it needs to round the number.

Thanks Stephen, is there any other way for it to do the sums accurately, with no rounding and cutting off after the 3rd digit from the decimal point please ?

Just use .toFixed(3) without the code to round it to 2 decimals first.

Thanks everyone for the help, but rounding is still going on ? ?

function convertee(){
    value = document.inputFormee.argument.value;
    if (isNaN(value)){
        alert(value+' is not a valid entry'); return;
    }
    arg = (value/1.75975326);
    arg = (parseInt(arg*100)/100).toFixed(3);
    document.inputFormee.result.value = arg;
}

Any help much appreciated. :slight_smile:

Dez.

Rounding can still occur even when using toFixed(). It depends on the value of the integer.