Connecting Tech Pros Worldwide Help | Site Map

Rounding off numbers

  #1  
Old August 16th, 2006, 06:15 PM
davidchase314@gmail.com
Guest
 
Posts: n/a
Hi. I was just wondering how I could get the output of this bit of
Javascript to round up to two decimal places. My code is below. Thank
you.

"<script language="JavaScript">

<!--

var price1 = 2.99
var price2 = 13.98
var price3 = 8.95
var price4 = 1.99

var oprice = price1 + price2 + price3 + price4

document.write(oprice)
//-->

</script>"

The output I'm getting is 27.909999999999996

Thanks again.

  #2  
Old August 16th, 2006, 07:05 PM
Evertjan.
Guest
 
Posts: n/a

re: Rounding off numbers


wrote on 16 aug 2006 in comp.lang.javascript:
Quote:
Hi. I was just wondering how I could get the output of this bit of
Javascript to round up to two decimal places. My code is below. Thank
you.
>
"<script language="JavaScript">
>
<!--
>
var price1 = 2.99
var price2 = 13.98
var price3 = 8.95
var price4 = 1.99
>
var oprice = price1 + price2 + price3 + price4
>
document.write(oprice)
//-->
>
</script>"
>
The output I'm getting is 27.909999999999996
The de facto FAQ:

<http://www.merlyn.demon.co.uk/js-round.htm>


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
  #3  
Old August 17th, 2006, 07:05 PM
Dr John Stockton
Guest
 
Posts: n/a

re: Rounding off numbers


JRS: In article <1155749041.610694.314930@m73g2000cwd.googlegroups .com>
, dated Wed, 16 Aug 2006 10:24:01 remote, seen in
news:comp.lang.javascript, davidchase314@gmail.com posted :
Quote:
I was just wondering how I could get the output of this bit of
>Javascript to round up to two decimal places.
In general, you want to round to nearest, not round up.

Before posting, you should read the newsgroup and its FAQ; your
situation is amply covered therein.

Quote:
>"<script language="JavaScript">
Omit first ". Attribute deprecated.
Quote:
><!--
>
>var price1 = 2.99
>var price2 = 13.98
>var price3 = 8.95
>var price4 = 1.99
>
>var oprice = price1 + price2 + price3 + price4
>
>document.write(oprice)
>//-->
>
></script>"
Omit ".
Quote:
>The output I'm getting is 27.909999999999996
Yes.

If you want an answer exact in pence, then you can work in pence.
Integers up to 2^53 are held exactly, but most decimal fractions are
not.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
about rounding off numbers flavourofbru answers 5 July 6th, 2007 10:00 PM
rounding off numbers Allen Thompson answers 22 July 20th, 2005 01:51 PM