Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Rounding errors with the ROUND()

 
   Database Help (Home) -> mySQL RSS
Next:  disable triggers for my (or any) session only  
Author Message
Matias Silva

External


Since: Feb 17, 2005
Posts: 2



(Msg. 1) Posted: Thu Feb 17, 2005 9:42 am
Post subject: Rounding errors with the ROUND()
Archived from groups: mailing>database>mysql (more info?)

Can anybody tell me why I am getting rounding errors
using the ROUND function.

3.7125 rounds to 3.70 when I use the following:
TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2)))
The correct value should be 3.71

I could round to the 3rd decimal place ROUND(X,3) and
that would round it correctly to 3.71 but that would
mean I would have to change the ROUND function in another
part of the query in order to make it uniform (to 3 decimal
places).

The kick in the ass is if I try to make it uniform (to 3
places) then another value is wrong and when this one
is right...

Appreciate any help, thanks,
Matias

 >> Stay informed about: Rounding errors with the ROUND() 
Back to top
Login to vote
tom173

External


Since: Dec 15, 2004
Posts: 7



(Msg. 2) Posted: Thu Feb 17, 2005 12:12 pm
Post subject: Re: Rounding errors with the ROUND() [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

"Matias Silva" wrote in message

 > Can anybody tell me why I am getting rounding errors
 > using the ROUND function.
 >
 > 3.7125 rounds to 3.70 when I use the following:
 > TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2)))
 > The correct value should be 3.71
 >
 > I could round to the 3rd decimal place ROUND(X,3) and
 > that would round it correctly to 3.71 but that would
 > mean I would have to change the ROUND function in another
 > part of the query in order to make it uniform (to 3 decimal
 > places).
 >
 > The kick in the ass is if I try to make it uniform (to 3
 > places) then another value is wrong and when this one
 > is right...

Why are you blaming ROUND()?

It appears that it is TRUNCATE() that is a bit funky and ROUND() does indeed
give the correct results according to it's definition.

ROUND(3.7125,2) = 3.71 <- correct
TRUNCATE(3.71, 2) = 3.70 <- *wrong* according to TRUNCATEs definition.

And - Since ROUND is a truncation to the number of decimal places you
specified anyway, why in heck do you need to TRUNCATE?

ROUND(x, n)
should *always* return the exact same value as the needlessly complicated -
TRUNCATE(ROUND(x, n), n))
because you have already discarded your extraneous digits using ROUND alone.

So why deal with the TRUNCATE() bug?
Even if it returned the correct result, it does nothing for you!

Although we do like to know about these bugs Smile
Thomas Bartkus

 >> Stay informed about: Rounding errors with the ROUND() 
Back to top
Login to vote
Matias Silva

External


Since: Feb 17, 2005
Posts: 2



(Msg. 3) Posted: Thu Feb 17, 2005 12:12 pm
Post subject: Re: Rounding errors with the ROUND() [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Thomas Bartkus wrote:


 >
  >>Can anybody tell me why I am getting rounding errors
  >>using the ROUND function.
  >>
  >>3.7125 rounds to 3.70 when I use the following:
  >>TRUNCATE(ROUND(units_pay_amount * fees_amount, 2),2)))
  >>The correct value should be 3.71
  >>
  >>I could round to the 3rd decimal place ROUND(X,3) and
  >>that would round it correctly to 3.71 but that would
  >>mean I would have to change the ROUND function in another
  >>part of the query in order to make it uniform (to 3 decimal
  >>places).
  >>
  >>The kick in the ass is if I try to make it uniform (to 3
  >>places) then another value is wrong and when this one
  >>is right...
 >
 >
 > Why are you blaming ROUND()?
 >
 > It appears that it is TRUNCATE() that is a bit funky and ROUND() does indeed
 > give the correct results according to it's definition.
 >
 > ROUND(3.7125,2) = 3.71 <- correct
 > TRUNCATE(3.71, 2) = 3.70 <- *wrong* according to TRUNCATEs definition.
 >
 > And - Since ROUND is a truncation to the number of decimal places you
 > specified anyway, why in heck do you need to TRUNCATE?
 >
 > ROUND(x, n)
 > should *always* return the exact same value as the needlessly complicated -
 > TRUNCATE(ROUND(x, n), n))
 > because you have already discarded your extraneous digits using ROUND alone.
 >
 > So why deal with the TRUNCATE() bug?
 > Even if it returned the correct result, it does nothing for you!
 >
 > Although we do like to know about these bugs Smile
 > Thomas Bartkus
 >
 >
 >
 >


Thanks for straightening me out, your absolutly right. When
I removed the TRUNCATE it works.

Thanks for help,
Matias
 >> Stay informed about: Rounding errors with the ROUND() 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Best way to issue hundreds of inserts/updates??? - Using mysql 4.0.23- What is the best way to execute several (hundreds of) inserts and updates? Rather than issuing tons of individual inserts and updates, can I send the strings to a text file and then have mysql do them all?? IE : query.txt insert..

MySQL freezes, brings XP machine to a grinding halt - I've been using MySQL for a while for fairly light database development on my XP machine. Currently, I am just starting a new project and have experienced some big problems with MySQL today both on my office machine and at home where running a particular...

login as user 'root' but do not have root privlages and my.. - Hi gang: I'm experiencing a problem with MySQL -- I updated MySQL from version 4.1.0 to 4.1.10 and now when I login as root it doesn't show all the databases I should have access to, nor it doesn't recognize me being logged in as root (via..

FLUSH TABLES hangs if table is locked - Using FLUSH TABLES via the C query API mysql_query() hangs if the table is locked already. That is to say, nothing prevents me from running a LOCK TABLES twice; it won't tell me "it's already locked, don't try to run a FLUSH". Anyone know ...

Tool to convert DDL in graphical diagram? - Does anyone know of a tool that will create a graphical diagram from a DDL (SQL create script)? Preferable a free open-source tool. Thanks, A
   Database Help (Home) -> mySQL All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]