|
hey guys.. i'm trying to make a form for work... we have to do some random math calculation that has to be very precise. say we have a number 2.69456324556356 I need to get that number rounded up to the nearest decimal point.. so it would be 2.69 i've tried to use Math.ceil() but it only will come up with 3 ... which is not what i'm looking for. anyone know what the best way to do this would be? EDIT: infact the whole math query seems to be bollacks lol... can someone convert this equation into javascript? a = 67.50 b = 104 c = b/a d = c rounded up to the nearest decimal point. e = d*(b-1) f = e-d anyone help? lol
Last Edit: Feb 13, 2009 21:02:10 GMT by Llanilek
|
|
|
|
|
1. c = Math.round((b/a)*100)/1002. d = c.toFixed(2)3. Math.roundNumber = function(a,b){ if(!isNaN(parseInt(a))){ c = Math.pow(10,b); return (Math.round(a*c)/c); } return false; }
d = Math.roundNumber(c,2)
Last Edit: Feb 13, 2009 13:21:27 GMT by crazynarutard
|
|
|
|
right i'll try and make sense of that lol... thanks cj lol
Last Edit: Feb 13, 2009 20:17:03 GMT by Llanilek
|
|
|
|
|
i posted 3 possibilities of doing this. First way is multiplying by 100 than dividing by 100. So if we had a number like: 2.356457843 and we multiplied that by 100, it'd be: 235.6457843 we then round that using Math.round, which would make it: 236 and then we divide it again by 100: 236/100 = 2.36
The second way to do this is using the method toFixed(arg), with arg being how many numbers you want behind the comma =p
The third one I posted is a simple function anyone can make, requires 2 arguments. The number you want to round and by how many decimals.
Use whichever method works for you ^_^
|
|
|
|
well u have never lost your touch of explaining things cj thanks again lol
|
|
|
|
|
I'd just have told you to use toFixed... nice thinking CJ.
|
|
|
|
|
tbh I didn't know about toFixed untill after i wrote 1 and 3, and then used google to find out if there was an easier way
|
|
|
|
tbh I didn't know about toFixed untill after i wrote 1 and 3, and then used google to find out if there was an easier way Go read up on toPrecision then too.
|
|
|
|
|
hmmm i dunno if this is a conflict in my code but none of them work lol....
well they do... but well its when you get to higher numbers it doesn't do it lol...
function processForm() {
var type = document.fm.type.value; var terms = document.fm.terms.value; var cprice = document.fm.cprice.value; var iprice = document.fm.iprice.value; var accept = new Array(); accept[0] = "104"; accept[1] = "156"; accept[2] = "208"; accept[3] = "260"; accept[4] = "312"; Math.roundNumber = function(a,b){ if(!isNaN(parseInt(a))){ c = Math.pow(10,b); return (Math.round(a*c)/c); } return false; }
// if var type = EXA if(type == "EXA") { if(in_array(terms, accept)) { var repay_multi = terms-1; var a = iprice/terms; var b = Math.roundNumber(a,2) var c = b*repay_multi; var d = iprice-c; var output = d;
alert(output); } }
}
|
|
|
|
|
it shoould work fine '-' I wonder if it's because of the a,b,c variables in the function and the ones you use for the numbers @.@ Does .toFixed() not work either?
|
|
|
|
they all do the same... possible that is effected by the other vars i'll try that n let u no edit: doesn't make any difference
Last Edit: Feb 17, 2009 23:32:59 GMT by Llanilek
|
|
|
|
|
Could you post the form that you're using too? Then we can test it & fix it! ^_^
|
|
|
|
|
lol you're silly.
if(type == "EXA") { if(in_array(terms, accept)) { var repay_multi = terms-1; var e = iprice/terms; var f = Math.roundNumber(e,2); alert(f) //gives: xx.xx var g = f*repay_multi; //xx.xx*repay_multi = xx.xxxxxxxxxxxxxxxx var d = iprice-g; var output = d;
lert(output); } } see comments, hope you get what i mean.
|
|
|
|
bingo lol cheers cj
|
|
|
|