Please login or register. Welcome to the Studio, guest!


Quick Links:


newBookmarkLockedFalling

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
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

crazynarutard

crazynarutard Avatar

*****
Senior Studio Member

1,470


August 2005
1. c = Math.round((b/a)*100)/100

2. 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

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
right i'll try and make sense of that lol...


thanks cj lol


Last Edit: Feb 13, 2009 20:17:03 GMT by Llanilek

crazynarutard

crazynarutard Avatar

*****
Senior Studio Member

1,470


August 2005
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 ^_^

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
well u have never lost your touch of explaining things cj thanks again lol

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
I'd just have told you to use toFixed... nice thinking CJ. :P

crazynarutard

crazynarutard Avatar

*****
Senior Studio Member

1,470


August 2005
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 :P

Chris

Chris Avatar

******
Head Coder

19,519


June 2005
crazynarutard Avatar
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 :P


Go read up on toPrecision then too. :P

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
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);

}
}

}


crazynarutard

crazynarutard Avatar

*****
Senior Studio Member

1,470


August 2005
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?

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
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

Michael

Michael Avatar
*Has a custom title*



1,462


October 2007
Could you post the form that you're using too? Then we can test it & fix it! ^_^

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005

crazynarutard

crazynarutard Avatar

*****
Senior Studio Member

1,470


August 2005
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.

Llanilek

Llanilek Avatar
Former Elite Mod

****
Dedicated Studio Member

931


October 2005
bingo lol cheers cj

newBookmarkLockedFalling