473,783 Members | 2,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to [properly] calculate sales commission in C/C++?

I have 10+ experience in C/C++ - mostly automation and graphics. I have
never written any business apps though. Recently I've been asked to write a
simple report that would calculate sales commission. The report needs to be
generated from within a C/C++ app. I don't want to mess it up so I thought
maybe someone from this group could give me some advice or point to me to a
place that'll provide some background on the issue.

Here is a description of the problem...
A salesperson sales items for a price (p). Each item category has a
commission rate (r) assigned to it. In a simple way I would calculate a
commission (c) as follows:
c = p * r and then round c to 2 decimal places.
The total for the report would be a sum of all commissions (rounded)
calculated for the items.

Is this the way to do that? Some item prices might be really low and I'm
concerned that floating point arithmetic that can get out of hand. Is
rounding to 2 decimal places a 'standard' in such cases?

I'd appreciate any suggestions,
Thanks
Jurek
Apr 6 '06 #1
7 4329
I have 10+ experience in C/C++ - mostly automation and graphics. I have
never written any business apps though. Recently I've been asked to write
a simple report that would calculate sales commission. The report needs
to be generated from within a C/C++ app. I don't want to mess it up so I
thought maybe someone from this group could give me some advice or point
to me to a place that'll provide some background on the issue.

Here is a description of the problem...
A salesperson sales items for a price (p). Each item category has a
commission rate (r) assigned to it. In a simple way I would calculate a
commission (c) as follows:
c = p * r and then round c to 2 decimal places.
The total for the report would be a sum of all commissions (rounded)
calculated for the items.

Is this the way to do that? Some item prices might be really low and I'm
concerned that floating point arithmetic that can get out of hand. Is
rounding to 2 decimal places a 'standard' in such cases?


If you have to add a long list of floating point values (the standard float
and doubles), the correct approach is to sort them beforehand so that you
add the smallest values first. That way the sum will be closest to the
'real' sum you should have because the precision error will be as small as
possible.

You can use doubles instead of floats to increase precision. that should be
fine for most applications. (simple billing , commsions etc)
Do a google for floating point precision and arithmetic precision if you
want to know the exact limits and errors you will have.

rounding to 2 digits is only done for the end result. not for the
intermediates, except perhaps for displaying purposes.

Of course, If you want to be correct up until the last hunderth of a cent,
you can use System::Decimal (.NET) which was made for financial calculations
that do not allow for rounding errors (bank apps and bookkeeping stuff).
That means you would have to use C++/CLI, though.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"
Apr 6 '06 #2
"Jurek" <no****@nospam. com> wrote in message
news:OP******** *****@TK2MSFTNG P05.phx.gbl...
I have 10+ experience in C/C++ - mostly automation and graphics. I have
never written any business apps though. Recently I've been asked to write
a simple report that would calculate sales commission. The report needs to
be generated from within a C/C++ app. I don't want to mess it up so I
thought maybe someone from this group could give me some advice or point to
me to a place that'll provide some background on the issue.

Here is a description of the problem...
A salesperson sales items for a price (p). Each item category has a
commission rate (r) assigned to it. In a simple way I would calculate a
commission (c) as follows:
c = p * r and then round c to 2 decimal places.
The total for the report would be a sum of all commissions (rounded)
calculated for the items.

Is this the way to do that? Some item prices might be really low and I'm
concerned that floating point arithmetic that can get out of hand. Is
rounding to 2 decimal places a 'standard' in such cases?


If your salespeople are not selling fleets of 747s or A300s, in a quick
one-off application I'd expect that most amounts would be held internally as
pennies in 32 bit "int"s. If that doesn't provide sufficient range you could
use the __int64 type.

Of course when it comes time to display the results, the dollar amount comes
from dividing the penny amount by 100, the cents amount is the remainder
modulo 100.

Regards,
Will

Regards,
Will


Apr 6 '06 #3
Bruno, thanks for your input. Unfortunately I can't use C++/CLI. In
regards to rounding... I think I might need to show commission on a per
item basis as well as a report total. If I do not round the commission at
the item level then the report total (i.e. the sum of item commissions)
might not always be correct 'visually' . For example, I might end up with
something like:

item1 0.21
item2. 2.99
item3 0.01
-----------
total 3.22

In reality I don't think that this would happen when the number of items is
so low. On the other hand, with 100ths of items it is almost certain that
the total will be off (visually) by a cent or more. Rounding at the item
level would eliminate the 'visual' problem but would compromise accuracy of
the total (which is probably more important).

Any idea what is appropriate in this case?

Thanks,
Jurek

"Bruno van Dooren" <br************ **********@hotm ail.com> wrote in message
news:Oh******** *****@TK2MSFTNG P04.phx.gbl...
I have 10+ experience in C/C++ - mostly automation and graphics. I have
never written any business apps though. Recently I've been asked to
write a simple report that would calculate sales commission. The report
needs to be generated from within a C/C++ app. I don't want to mess it
up so I thought maybe someone from this group could give me some advice
or point to me to a place that'll provide some background on the issue.

Here is a description of the problem...
A salesperson sales items for a price (p). Each item category has a
commission rate (r) assigned to it. In a simple way I would calculate a
commission (c) as follows:
c = p * r and then round c to 2 decimal places.
The total for the report would be a sum of all commissions (rounded)
calculated for the items.

Is this the way to do that? Some item prices might be really low and I'm
concerned that floating point arithmetic that can get out of hand. Is
rounding to 2 decimal places a 'standard' in such cases?


If you have to add a long list of floating point values (the standard
float and doubles), the correct approach is to sort them beforehand so
that you add the smallest values first. That way the sum will be closest
to the 'real' sum you should have because the precision error will be as
small as possible.

You can use doubles instead of floats to increase precision. that should
be fine for most applications. (simple billing , commsions etc)
Do a google for floating point precision and arithmetic precision if you
want to know the exact limits and errors you will have.

rounding to 2 digits is only done for the end result. not for the
intermediates, except perhaps for displaying purposes.

Of course, If you want to be correct up until the last hunderth of a cent,
you can use System::Decimal (.NET) which was made for financial
calculations that do not allow for rounding errors (bank apps and
bookkeeping stuff).
That means you would have to use C++/CLI, though.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"

Apr 6 '06 #4
"Bruno van Dooren" <br************ **********@hotm ail.com> wrote in message
news:Oh******** *****@TK2MSFTNG P04.phx.gbl...
Of course, If you want to be correct up until the last hunderth of a cent,
you can use System::Decimal (.NET) which was made for financial
calculations that do not allow for rounding errors (bank apps and
bookkeeping stuff).
That means you would have to use C++/CLI, though.


Actually, System.Decimal is nothing more than a wrapper over the OLE
Automation DECIMAL type, which you can use in native C++ programs.

See the functions VarDec* in MSDN at

http://msdn.microsoft.com/library/en...asp?frame=true

-cd
Apr 6 '06 #5
> Bruno, thanks for your input. Unfortunately I can't use C++/CLI. In
regards to rounding... I think I might need to show commission on a per
item basis as well as a report total. If I do not round the commission at
the item level then the report total (i.e. the sum of item commissions)
might not always be correct 'visually' . For example, I might end up with
something like:

item1 0.21
item2. 2.99
item3 0.01
-----------
total 3.22
In reality I don't think that this would happen when the number of items
is so low. On the other hand, with 100ths of items it is almost certain
that the total will be off (visually) by a cent or more. Rounding at the
item level would eliminate the 'visual' problem but would compromise
accuracy of the total (which is probably more important).

Any idea what is appropriate in this case?


If you have several items, it is bound to happen as you say.
Increasing number of digits displayed will not solve that. it will only make
it less obvious, and noticeable only when you have a larger number of items.

It hink the correct approach would be to only display as much decimals as
exist in reality for your currency (0,01 $ or 0.01 Euro for example).

Your end result should be calculated with the'real' values and might end up
not 100% the sum of the individual items, but that cannot be helped. I think
the best you could do would be to place a small note somewhere (or perhaps
mention it in a tooltip popup or help item) that the visual representation
might be a few cents off, but that the end result will be correct. And then
add an explanation of why this is the case.
That way your users will know what is going on if they should ask themselves
the question.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"
Apr 6 '06 #6
Will, thanks for the suggestion. I have to say that I was thinking of going
with pennies only. The 32bit int is more than I need in this case. My
concern is that I'd still face rounding problems at some point. If, let's
say, a commission on an item is 7% and the price is 12 pennies (0.12) then
the commission amount calculated as:
c = 12 * 7 / 100 will not be very accurate if c is a 32-bit int. Unless,
of course, this is generally acceptable.
Any ideas?

Thanks,
Jurek
"William DePalo [MVP VC++]" <wi***********@ mvps.org> wrote in message
news:Ou******** ******@TK2MSFTN GP05.phx.gbl...
"Jurek" <no****@nospam. com> wrote in message
news:OP******** *****@TK2MSFTNG P05.phx.gbl...
I have 10+ experience in C/C++ - mostly automation and graphics. I have
never written any business apps though. Recently I've been asked to write
a simple report that would calculate sales commission. The report needs
to be generated from within a C/C++ app. I don't want to mess it up so I
thought maybe someone from this group could give me some advice or point
to me to a place that'll provide some background on the issue.

Here is a description of the problem...
A salesperson sales items for a price (p). Each item category has a
commission rate (r) assigned to it. In a simple way I would calculate a
commission (c) as follows:
c = p * r and then round c to 2 decimal places.
The total for the report would be a sum of all commissions (rounded)
calculated for the items.

Is this the way to do that? Some item prices might be really low and I'm
concerned that floating point arithmetic that can get out of hand. Is
rounding to 2 decimal places a 'standard' in such cases?


If your salespeople are not selling fleets of 747s or A300s, in a quick
one-off application I'd expect that most amounts would be held internally
as pennies in 32 bit "int"s. If that doesn't provide sufficient range you
could use the __int64 type.

Of course when it comes time to display the results, the dollar amount
comes from dividing the penny amount by 100, the cents amount is the
remainder modulo 100.

Regards,
Will

Regards,
Will

Apr 6 '06 #7
"Jurek" <no****@nospam. com> wrote in message
news:uc******** *******@TK2MSFT NGP05.phx.gbl.. .
My concern is that I'd still face rounding problems at some point.
Yes, but that is life. In most environments we don't split pennies. So, as
always, a good plan is to defer the rounding until as late as possible in
the life of a transaction. That minimizes the error. And depending on the
application, _best_ case is an error of no more than .5 cents per
transaction.
If, let's say, a commission on an item is 7% and the price is 12 pennies
(0.12) then the commission amount calculated as:
c = 12 * 7 / 100 will not be very accurate if c is a 32-bit int. Unless,
of course, this is generally acceptable.
Any ideas?


Well, you could treat percentages as I propose treating dollar amounts -
scale them up and defer the division until the very end (which narrows the
range of amounts you can deal with, btw)

Of course, it helps to know about the kinds of transactions your application
will process. IIRC the NYSE for many years quoted prices of shares of stock
in eighths of a dollar. As there were no trades on the floor except in
blocks of 100 shares, the fact that a single share may have cost $1.125 was
not cause for concern between "specialist s" who moved bazillions of shares a
day because 100 such shares would could $11.25. There was _never_ the
possibility of small rounding errors cascading into something large.

A problem arose only when granny sold her 10 or 12 share portfolio. Then
there was rounding error when the broker / exchange aggregated these small
orders transactions to execute the transaction. But with such a small number
of shares for granny the error for her is "in the noise".

In the end what you do will be determined by the numbers and kinds of
transactions that you expect to handle.

Regards,
Will

Apr 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1633
by: Ciar?n | last post by:
I have about 7 tables I need to join, but am having a lot of difficulty with the joins, that I need some help on. I'll provide the details of four tables to illustrate the scenario. I have one table called Product, which contains a complete list of products. I have another table called Date, which contains a complete list of dates. I have a table called sales, which contains Product, date and Qty.
0
1430
by: J-T | last post by:
Thanks Here is what I am trying to do without using database : These is a simple application with the description below, can someone help me on how to create classes,collections, .... for this: Basic sales tax is applicable at a rate of 10% on all goods, except books, food, and medical products that are exempt. Import duty is an additional sales tax applicable on all imported goods at a rate of 5%, with no exemptions.
0
1196
by: mike.mcnulty | last post by:
Canadian-based IT consulting firm, specializing in implementing Microsoft .NET technologies, seeks individuals for commission referral-based leads. We are looking for individuals that may have access to people who are decision makers or can access decision makers who require custom software solutions (i.e.-custom web apps, desktop apps, Windows SharePoint Services etc.). If you are able to refer them to us and we
0
2107
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I need the slope (representing growth) of monthly sales. the data is setup in a crosstab with the months in different collums, say column 1 to 12 for last year. I want to calculate the slope for every row (on the same query or another if needed)
5
7631
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I need the slope (representing growth) of monthly sales. the data is setup in a crosstab with the months in different collums, say column 1 to 12 for last year. I want to calculate the slope for every row (on the same query or another if needed)
0
1535
by: rabraham | last post by:
I have a commission report I’m working on and it is causing to lose hair fast. I need help customizing this commission report. The current report has a couple conditions: Condition 1: Pay 2% commission of the payment amount if purchased in 2006 Condition 2: Pay 5% commission of the payment amount if purchased in 2007 The new report needs to calculate commission base on the type of item purchased, and the previous conditions.
0
4641
by: SuzK | last post by:
I am trying to calculate in VBA in Access 2002 a moving average and update a table with the calculations. Fields in my WeeklyData table are Week Ending (date) ItemNbr (double) Sales Dollars (double) Sales Units (double) Promo (Text) -- is null or "X" AvgWklyDollars (double) AvgWklyUnits (double) I have a vba module which I thought would work, but it doesn't. I think the problem is an embedded SQL Totals Top 8 query, which doesn't...
12
4929
by: spima05 | last post by:
Hello I am developing a database to calculate commissions on a sale for each rep involved in the same and their uplines. Below is the database structure and the commissions schedule. I am having trouble designing a way to store the commission rates and calculate the commission amounts. Below is a list of the business rules for calculating the commisssions.
3
8421
by: lizBeth | last post by:
Hi all, i seem to have gotten stuck on this coursework, i am unsure as to how to implement a method in the main class to calculate the sum of all employees monthly salaries. Everything else works on the program it does output the employees details and their monthly salaries. I just need to add them up and output a "Total Payroll: xxxxxx.x " in this format. It sounds simple and it probably is simple, i think i can't see the woods for...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5379
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.