472,141 Members | 1,336 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Rounding errors with the ROUND()

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
Jul 23 '05 #1
2 2999
"Matias Silva" <ma**@nospam.com> wrote in message
news:11*************@corp.supernews.com...
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 :-)
Thomas Bartkus


Jul 23 '05 #2
Thomas Bartkus wrote:
"Matias Silva" <ma**@nospam.com> wrote in message
news:11*************@corp.supernews.com...
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 :-)
Thomas Bartkus

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

Thanks for help,
Matias
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

20 posts views Thread by Raoul Watson | last post: by
2 posts views Thread by Christopher T King | last post: by
3 posts views Thread by Dalan | last post: by
6 posts views Thread by Jeff Boes | last post: by
6 posts views Thread by shaqattack1992-newsgroups | last post: by
206 posts views Thread by md | last post: by
20 posts views Thread by jacob navia | last post: by
30 posts views Thread by bdsatish | last post: by
reply views Thread by leo001 | last post: by

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.