Click here to Skip to main content
15,896,118 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I would like to calculate a selling price based on field 1 (the purchase price) and field2 (storage in pergentages). What am I doing wrong with the formula below.

Simple sum field1 = 1.00 x 30 field two (30%) = field3

C#
<pre>
int a, b;

bool isAValid = int.TryParse(XX_ field1.Text, out a);
bool isBValid = int.TryParse(XX_ field2.Text, out b);

                            if (isAValid && isBValid)
                            {
                                LB_verkoopprijs.Text = (a * b).ToString();
                            }

                            else
                            {
                                LB_verkoopprijs.Text = "Invalid input";
                            }
Posted

1 solution

Two possibilities:
1) The spaces in your source code are preventing it compiling properly:
C#
bool isAValid = int.TryParse(XX_ field1.Text, out a);
bool isBValid = int.TryParse(XX_ field2.Text, out b);

Should probably be:
C#
bool isAValid = int.TryParse(XX_field1.Text, out a);
bool isBValid = int.TryParse(XX_field2.Text, out b);

Or
2) The textboxes don't contain integer numbers.
Your example suggests that you are entering "1.00" as "field1" which will not parse as an integer.
Try:
C#
double a, b;
 
bool isAValid = double.TryParse(XX_ field1.Text, out a);
bool isBValid = double.TryParse(XX_ field2.Text, out b);
And you might get something better.

But to get 30% of something, you don't want "a * b": you need "a * (b / 100.0)" for 30% of the purchase price, or "a * (1.0 + b / 100.0)" for the purchase price plus 30%
 
Share this answer
 
Comments
MaikelO1 28-Dec-15 10:38am    
Can you explain this by a formula; "But to get 30% of something, you don't want "a * b": you need "a * (b / 100.0)" for 30% of the purchase price, or "a * (1.0 + b / 100.0)" for the purchase price plus 30%"
OriginalGriff 28-Dec-15 10:43am    
You are kidding me, yes?
What is 30% of 50?
Is it:
A) 15?
or
B) 1500?
50 * 30 == 1500
50 * (30 / 100) == 15

Now your turn. What is "50 plus 30%"?

MaikelO1 28-Dec-15 10:59am    
I had forgotten to take the "1.0" across. Therefore, the results did not match.. Sorry and thanks for the solution
OriginalGriff 28-Dec-15 11:06am    
:laugh:
We've all done it!
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900