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
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.
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().
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 ;-(
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.
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;
}
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 ?
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;
}