473,378 Members | 1,139 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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.

Aug 16 '06 #1
2 4146
wrote on 16 aug 2006 in comp.lang.javascript:
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)
Aug 16 '06 #2
JRS: In article <11**********************@m73g2000cwd.googlegroups .com>
, dated Wed, 16 Aug 2006 10:24:01 remote, seen in
news:comp.lang.javascript, da***********@gmail.com posted :
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.

>"<script language="JavaScript">
Omit first ". Attribute deprecated.
><!--

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 ".
>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.
Aug 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ron Adam | last post by:
With all the discussion about numbers here and particularly in regards to prePEP concerning decimal data types got me wondering about how numbers are handled when it comes to rounding. And if that...
2
by: Christopher T King | last post by:
Speakign of rounding errors, why is round() defined to round away from zero? I've always been quite fond of the mathematical definition of rounding of floor(n+.5). As a contrived example of why the...
3
by: Caesar Augustus | last post by:
Hello Tom, Long time listener, first time caller... I have been working with vb code to automate a salary increase process that not only rounds to the nearest penny but also rounds to the...
13
by: dhildebrandt | last post by:
I have a query that uses the Round function to change decimals into whole numbers. For 4 years straight the thing has always rounded the numbers in exactly the same way so that whenever I update...
3
by: Norvin Laudon | last post by:
Hi, Can somebody explain the following, from the MSDN documentation for the "System.Convert.ToInt32(double)" function <quote> Return Value value rounded to the nearest 32-bit signed...
9
by: Joe Attardi | last post by:
Hi all, Math is not my strongest area so forgive me if I use some of the wrong terminology. It seems that scientific notation is immune to rounding errors. For example: (4.98 * 100) + 5.51 ...
11
by: cj | last post by:
Lets assume all calculations are done with decimal data types so things are as precise as possible. When it comes to the final rounding to cut a check to pay dividends for example in VB rounding...
29
by: Marco | last post by:
Hello, I have : float f = 36.09999999; When I do : char cf; sprintf(cf,"%0.03lf", f); I get : 36.100
206
by: md | last post by:
Hi Does any body know, how to round a double value with a specific number of digits after the decimal points? A function like this: RoundMyDouble (double &value, short numberOfPrecisions) ...
30
by: bdsatish | last post by:
The built-in function round( ) will always "round up", that is 1.5 is rounded to 2.0 and 2.5 is rounded to 3.0. If I want to round to the nearest even, that is my_round(1.5) = 2 # As...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.