[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Rounding to nearest cent in delphi

Posted on 2008-02-06
8
Medium Priority
?
1,201 Views
Last Modified: 2010-04-21
The ability to round in Delphi is none existant. I am working on a payroll app that needs amounts rounded to the nearest .01. For example, an amount of 12.7875 must be rounded to 12.79. An amount of 14.7375 must round to 14.74. Another example of an amount of 1.754 must round to 1.75.

I have tried numerous functions within Delphi and some found on the Internet and nothing is working. Thanks in advance!
0
Comment
Question by:herrschlaumeier
  • 4
  • 2
  • 2
8 Comments
 
LVL 19

Expert Comment

by:MerijnB
ID: 20835960
see this
function RoundCents(Amount: Double): Double;
begin
 result := Trunc((Amount * 100) + 0.49999999999999999) / 100;
end;
 
procedure TForm4.Button1Click(Sender: TObject);
begin
 Caption := floattostr(roundcents(14.7375));
end;

Open in new window

0
 

Author Comment

by:herrschlaumeier
ID: 20836138
That worked for quite a few but it fails on 7.395, 27.9850. It looks like when the 1000th column is 5 it fails to round up.
0
 
LVL 19

Accepted Solution

by:
MerijnB earned 2000 total points
ID: 20836192
try this one:
function RoundCents(Amount: Double): Double;
begin
 Amount := Amount * 100;
 Amount := Amount + 0.5;
 Amount := trunc(Amount);
 Amount := Amount / 100;
 
 result := Amount;
end;

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:herrschlaumeier
ID: 20836238
Thanks for your quick replies...

That solved those two and all the others still work. However, I tried 123.8345 to see if it would round to 123.84 and it didn't. Also, I did 1.015 and it gave me 1.01 but if I did 1.0151 it gave me 1.02.
0
 
LVL 85

Expert Comment

by:ozo
ID: 20836307
binary floating point numbers can't store numbers like 7.395, 27.9850, 1.015  exactly, and wll instead store rounded values like
7.3949999999999995736743585439398884773254
27.9849999999999994315658113919198513031006
1.0149999999999999023003738329862244427204
123.8345000000000055706550483591854572296143

0
 

Author Comment

by:herrschlaumeier
ID: 20836358
I did find that the last post worked when I used the SetRoundMode to rmUp. I only have 1 that doesn't work now  (123.8345 ) and frankly, I get paid hourly so these fractions of pennies will never amount to anything for me:) Thanks for the help.
0
 

Author Closing Comment

by:herrschlaumeier
ID: 31428659
I added the SetRoundMode(rmUp) and it worked for all values rounding from the .000x place and up.
0
 
LVL 85

Expert Comment

by:ozo
ID: 20836384
123.8345 rounded to the nearest 0.01 is 123.8300
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

640 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question