Rounding to nearest cent in delphi

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!
herrschlaumeierAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MerijnBSr. Software EngineerCommented:
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
herrschlaumeierAuthor Commented:
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
MerijnBSr. Software EngineerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

herrschlaumeierAuthor Commented:
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
ozoCommented:
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
herrschlaumeierAuthor Commented:
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
herrschlaumeierAuthor Commented:
I added the SetRoundMode(rmUp) and it worked for all values rounding from the .000x place and up.
0
ozoCommented:
123.8345 rounded to the nearest 0.01 is 123.8300
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.