Closed Thread Icon

Preserved Topic: how do you round to 2 decimal places? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=18421" title="Pages that link to Preserved Topic: how do you round to 2 decimal places? (Page 1 of 1)" rel="nofollow" >Preserved Topic: how do you round to 2 decimal places? <span class="small">(Page 1 of 1)</span>\

 
hecster2k
Nervous Wreck (II) Inmate

From: sj, ca, usa
Insane since: Feb 2002

posted posted 09-18-2002 23:38

how do you round to 2 decimal places?

"there has to be a solution."

InI
Paranoid (IV) Mad Scientist

From: Somewhere over the rainbow
Insane since: Mar 2001

posted posted 09-18-2002 23:49

The poster has demanded we remove all his contributions, less he takes legal action.
We have done so.
Now Tyberius Prime expects him to start complaining that we removed his 'free speech' since this message will replace all of his posts, past and future.
Don't follow his example - seek real life help first.

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-18-2002 23:56

Personally, I'd multiply by 100, round off (Math.round()), and then divide by 100.

function roundoff(num, decimalplaces)
{
var decimalfactor = Math.pow(10,decimalplaces);
return Math.round(num*decimalplaces)/decimalplaces;
}

hecster2k
Nervous Wreck (II) Inmate

From: sj, ca, usa
Insane since: Feb 2002

posted posted 09-19-2002 01:31

if you were to multiply and divide by 100, in the case of a '0' ending up at the end (for example, 12.50), it would show up as 12.5 and not 12.50. i need to maintain a consistency with regards to the monetary figures.

"there has to be a solution."

hecster2k
Nervous Wreck (II) Inmate

From: sj, ca, usa
Insane since: Feb 2002

posted posted 09-19-2002 01:48

actually, i did a little indexOf workaround magic and solved my problem. thanks for the head start slime!

-gerald

"there has to be a solution."

genis
Paranoid (IV) Inmate

From: Dallas, TX
Insane since: Aug 2002

posted posted 09-19-2002 04:11

oh come on hecster... how am I suppose to learn from what you did if you don't post it?

Please everyone, always post your solution so as to not hang later readers out to dry.

Thanks.

genis
Paranoid (IV) Inmate

From: Dallas, TX
Insane since: Aug 2002

posted posted 09-19-2002 04:44

hmm... I didn't know IE had implemented .toFixed but it seems to work.

code:
function precise(num,X) { //num to X decimal places, defaults to 2
X = (!X ? 2 : X);
if(Number.prototype.toFixed)
return num.toFixed(X);
else
return Math.round(num*Math.pow(10,X))/Math.pow(10,X);
}

When given 123.4 the above function should give back 123.40 for more current browsers that implement Number.toFixed and 123.4 for older ones.



[This message has been edited by genis (edited 09-19-2002).]

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-19-2002 07:23

um... I don't know much about the "Math.round()" function, but shouldn't it do something like " Math.round(-2)" for two decimal places?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-19-2002 07:52

Math.round() just rounds a number; for instance, Math.round(1.3) returns 1, and Math.round(5.7) returns 6.

BTW petskull: I dropped Trillian at least for now 'cause I didn't like the interface. Sorry... =)

Petskull
Maniac (V) Mad Scientist

From: 127 Halcyon Road, Marenia, Atlantis
Insane since: Aug 2000

posted posted 09-19-2002 17:00

no prob, dude... you want that ol' version of ICQ98?


Code - CGI - links - DHTML - Javascript - Perl - programming - Magic - http://www.twistedport.com
ICQ: 67751342

Slime
Lunatic (VI) Mad Scientist

From: Massachusetts, USA
Insane since: Mar 2000

posted posted 09-19-2002 17:13

No thanks, I think I had that originally, didn't really like *it* either =)

hecster2k
Nervous Wreck (II) Inmate

From: sj, ca, usa
Insane since: Feb 2002

posted posted 09-20-2002 01:36

oh, oops ... hehe. here, have a look. it seems to do the trick for me. added a little more code to Slime's code.

code:
function roundOff(num, decimalplaces)
{
var decimalfactor = Math.pow(10,decimalplaces);
roundedValue = Math.round(num*decimalfactor)/decimalfactor;
strVal = roundedValue.toString();

if (strVal.indexOf(".") == -1)
roundedValue += ".00";
else if (strVal.substring(strVal.indexOf(".")+1,strVal.length).length < 2)
roundedValue += "0";

return roundedValue;
}



"there has to be a solution."

Hugh
Paranoid (IV) Inmate

From: Dublin, Ireland
Insane since: Jul 2000

posted posted 09-20-2002 18:45

Would this do ? :
x = parseInt((x*100))\100;

« BackwardsOnwards »

Show Forum Drop Down Menu