Discussion:
debug.print 183 - 173.55
(too old to reply)
xgold
2006-11-28 22:19:40 UTC
Permalink
i have visual basig 6

if i type in the debug window it gives me a strange result

debug.print 183 - 173.55
9.44999999999999

why this result instead of 9.45

the same is happening if i do
a = 183
b = 173.55
c = a-b
debug.print c

if i do 183 - 174.55 or 183 - 180.55 the same strange thing happen. ( with
.449999999 instead of .45 )why and how to avoid this?
if you try this with windows standard calculator it gives the right answer.

ty
Ken Halter
2006-11-28 23:20:40 UTC
Permalink
Post by xgold
i have visual basig 6
if i type in the debug window it gives me a strange result
debug.print 183 - 173.55
9.44999999999999
why this result instead of 9.45
This question is as old as floating point math itself
Post by xgold
if you try this with windows standard calculator it gives the right answer.
The Calculator knows how to work with floating point numbers.

Weird floating point problem
http://groups.google.com.my/group/microsoft.public.vb.general.discussion/msg/74b7abbe3645bd1b
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm
xgold
2006-11-29 01:59:12 UTC
Permalink
hmm strange problem , i didnt knew about that, i solved the problem using
the round function round(c,2) , ty.
Post by Ken Halter
Post by xgold
i have visual basig 6
if i type in the debug window it gives me a strange result
debug.print 183 - 173.55
9.44999999999999
why this result instead of 9.45
This question is as old as floating point math itself
Post by xgold
if you try this with windows standard calculator it gives the right answer.
The Calculator knows how to work with floating point numbers.
Weird floating point problem
http://groups.google.com.my/group/microsoft.public.vb.general.discussion/msg
/74b7abbe3645bd1b
Post by Ken Halter
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm
Robert Morley
2006-11-29 18:46:24 UTC
Permalink
If you're doing this in VB instead of via a query, you can also force this
to a Currency data type through a variety of methods...the simplest one (to
use your example) is:

debug.print 183@ - 173.55@

(Technically only one of the @'s is needed, but it's best to be thorough.)

If you don't mind fairly slow code, you can also use the Decimal data type:

debug.print CDec(183) - CDec(173.55)



Rob
Post by xgold
hmm strange problem , i didnt knew about that, i solved the problem using
the round function round(c,2) , ty.
Post by Ken Halter
Post by xgold
i have visual basig 6
if i type in the debug window it gives me a strange result
debug.print 183 - 173.55
9.44999999999999
why this result instead of 9.45
This question is as old as floating point math itself
Post by xgold
if you try this with windows standard calculator it gives the right answer.
The Calculator knows how to work with floating point numbers.
Weird floating point problem
http://groups.google.com.my/group/microsoft.public.vb.general.discussion/msg
/74b7abbe3645bd1b
Post by Ken Halter
--
Ken Halter - MS-MVP-VB - Please keep all discussions in the groups..
In Loving Memory - http://www.vbsight.com/Remembrance.htm
Robert Morley
2006-11-30 02:52:04 UTC
Permalink
Post by Robert Morley
If you're doing this in VB instead of via a query, you can also force this
Sorry about the reference to a query...most of my groups are Access groups
and I hadn't clued in that I was now in the realms of VB only. :)


Rob

Loading...