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.