Closed Thread Icon

Preserved Topic: Round Pages that link to <a href="https://ozoneasylum.com/backlink?for=7859" title="Pages that link to Preserved Topic: Round" rel="nofollow" >Preserved Topic: Round\

 
Author Thread
Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 10-01-2001 11:59

Hi everybody!

Thanks for the help on RegExps on my last post! (max, lallous, and everyone)

I have a question about: math.round

I have a number: 199,23732323 for en example.
I want it to be: 199,24

I can´t find out how I round it to 2 decimals.

Thanks!
Smash

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-01-2001 13:01

here you go smash...


code:
function my_round(num, dec)
{
if (String(num).match(/(-*[0-9]+)\.([0-9]+)/))
return parseFloat(RegExp.$1 + "." + Math.round(parseFloat(RegExp.$2.substr(0, dec) + "." + RegExp.$2.substr(dec,1))));
else
return num;
}

document.write(my_round(199.23732323,3));



Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 10-01-2001 13:43

Hi Lallous!

Thanks for the function. I have one problem though with it.....
If the number is for an example: 1,01666666666667
It will be converted to: 1,17

Which is pretty wrong

/Smash

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-01-2001 14:01

yes I noticed


I'll try to work on it/ fix it...but what you're asking for does not exist in any of javascript's builtin objects.

btw, explain me what you want exactly?!

what should this round to 1,01666666666667?
1,017 ?

[This message has been edited by lallous (edited 10-01-2001).]

Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 10-01-2001 14:14

No, 1.017666666666 should be 1.02

2 decimals. x.xx

If u understand

Thanks

Smash

lallous
Paranoid (IV) Inmate

From: Lebanon
Insane since: May 2001

posted posted 10-01-2001 14:37

try this,

code:
function my_round(num, dec)
{
if (String(num).match(/(-*[0-9]+)\.([0-9]+)/))
{
t = Math.round(parseFloat(RegExp.$2.substr(0, dec) + "." + RegExp.$2.substr(dec,1)));
if (t<9) t = String("0" + t);
return RegExp.$1 + "." + t;
}
else
return num;
}

function test_round(n){document.write("myround(" + n + ")=" + my_round(n, 2) + "<br>");}

test_round(1.017666666666);
test_round(1.01666666666667);
test_round(199.23732323);



still don't get how your 2 decimal rounding works...as far as i know round rounds a number and return a natural number.

VelociRaptor
Nervous Wreck (II) Inmate

From:
Insane since: Sep 2001

posted posted 10-01-2001 14:37

Not sure if I understood the question right, probably not, because this
sounds real simple.

Multiple the original number by 10 for 1 decimal, or 100 for 2 places, 1000 for 3 etc.
Use Math.round for the result
Divide the result by 10, by 100 for 2 places etc.

or try this one

function roundit(Num, Places) {
if (Places > 0) {
if ((Num.toString().length - Num.toString().lastIndexOf('.')) > (Places + 1)) {
var Rounder = Math.pow(10, Places);
return Math.round(Num * Rounder) / Rounder;
}
else return Num;
}
else return Math.round(Num);
}


as in roundit(102.3344432, 2)
(script by Jason Mood)

but i think the first method, which i've used in other languages, is the simplest.

[This message has been edited by VelociRaptor (edited 10-01-2001).]

Smash
Bipolar (III) Inmate

From: Stockholm, Sweden
Insane since: Sep 2001

posted posted 10-01-2001 15:45

Since I am getting mistunderstood all the time I am gonna try to explain the problem once again.

I am doing a calculation of time between two dates.... and from that calculation I get the result in hours.

Lets say I get 10,34555555555555 hours.... Then I don´t wanna display so many decimals, I just wanna display 2 decimals.

So in that example i want it to be rounded to 10.35

And since I get different numbers all the time, I can´t do a manual round.

And that´s why I want a simple function that works for such things.

The functions that I got from you guys are little buggey

Thanks, Smash!


Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 10-01-2001 15:46

Velociraptor is correct, that's the easiest way.

num = 105.01666666666
num = num * 100;
num = Math.round(num);
num = num / 100;

« BackwardsOnwards »

Show Forum Drop Down Menu