TIP: Click on subject to list as thread! ANSI
echo: delphi
to: ROBERT WILKINSON
from: GARY WEINFURTHER
date: 1997-10-05 08:50:00
subject: Floating Point

Robert Wilkinson mentioned this to ALL:
 RW> Am working on a billing program for the office where I work, and the 
lan
 RW> is once it's completed to release the program shareware.  The problem is
 RW> that when whole dollar amounts are entered it works as desireed, however
 RW> with uneven dollar amounts the decimal portion is being mangled.  For
 RW> example 27.90 entered in an edit box is being recorded as 
27.8999999999942
 RW> and 801.08 is being recorded as 801.080000000075.
This is a common problem with computers, and it's not specific to Delphi.  
The problem is with floating point numbers in general.  The way they are 
stored prevents every possible value from being accurately represented.  So 
in some cases, you'll get the next best thing.
The solution is to avoid using floating point values for dollar amounts.  I 
don't recall if Delphi 1 supports a Currency data type, but Delphi 2 and 3 
both do.  The Currency data type is internally a scaled integer value, scaled 
to four decimal places, so you will not have this problem.  If Delphi 1 does 
not have the Currency data type, then scale all your values by 100 and store 
them in long integers.  You should be able to create a simple class that does 
this automatically for you.
                                ...Gary
--- GoldED 2.41
---------------
* Origin: The Flying Circus BBS, Farmington Hills, MI, USA. (1:2410/905)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.