Connecting Tech Pros Worldwide Help | Site Map
 
 
LinkBack Thread Tools Search this Thread
  #1  
Old August 16th, 2006, 06:15 PM
davidchase314@gmail.com
Guest
 
Posts: n/a
Default Rounding off numbers

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

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 205,248 network members.