Need help computing correct ratio - c#

User enters gross sales, sales return and cost of goods sold into a program as followed:
**Program Demo**
Enter Gross sales: 1500000
Enter sales return: 100000
Enter cost of goods sold: 1000000
Gross Profit Ratio is >> 28.6%
Here is an illustration on ratio calculation: ****To illustrate the gross profit ratio, let's assume that a company has net sales of $800,000 and its cost of goods sold is $600,000. This means its gross profit is $200,000 (net sales of $800,000 minus its cost of goods sold of $600,000) and its gross profit ratio is 25% (gross profit of $200,000 divided by net sales of $800,000).
The illustration is saying net sales - cost of goods sold = gross profit, then gross profit / net sales.
The issue I have is that when I do the calculation for Gross Profit Ratio, the program I made returns a ratio of 33.33%...
In the calculation method I have:
public double CalculateRatio()
{
return System.Math.Round((grossSales - goodsSold) / grossSales * 100, 2);
}
So essentially (1500000 - 1000000) / 1500000 * 100 = 33.33...
How do I get the correct calculation?
I set calculation method to "(grossSales - goodsSold) / grossSales * 100" as described in the illustration but I have the wrong calculation when following the program demo..

Related

Weekly Loan payment/ Amortization Schedule Calculation logic in c#

I have completed Loan Amortization/ Loan Repayment calculation for monthly. Now i required for weekly calculation formula in c#.
Can any one please help me to answer this?
Here loan amount is 10000, Interest Rate is 10% and Terms 7 Months
This is my monthly calculation. but i need weekly calculation like this
loanAmount = 10000;
var periods = 7; //Months
var monthlyRate = (Convert.ToDouble(10) / 100) / 12;
var monthlyPayment = Math.Round(monthlyRate / (1 - Math.Pow((1 + monthlyRate), -(periods))) * loanAmount ,2);
Thanks in advance,
Mani
Without seeing exactly how you are calculating it, it should be as simple as switching out months for weeks. For example, instead of something like this:
monrthly payments = (balance * APR) / 12 months
Do this:
weekly payments = (balance * APR) / 52 weeks
Obviously you'll change it depending on what you're calculating and how you're calculating it. You've already done the hard part now you just have to change the time frame, does that make sense?

decimal price after appliying percent/tax

Helo,
I am making an application for managing budgets in C#, but I have many problems with rounding value. The specification I got was that the user can input the value of the product with and without tax (in two textboxes).
So I'm making a function to convert the price from price with tax to price without tax and price without tax to price with tax.
So I attached it to the TextChanged event of the two textboxes
private void txtPrecioTotal_TextChanged(object sender, EventArgs e)
{
if (!ignoreTextChanged)
{
decimal precio;
if (decimal.TryParse(txtPrecioTotal.Text, out precio))
{
precio = precio / (1 + IVATxt.Value / 100);
ignoreTextChanged = true;
txtPrecioBase.Text = precio.ToString("##.##");
ignoreTextChanged = false;
}
}
}
private void txtPrecioBase_TextChanged(object sender, EventArgs e)
{
if (!ignoreTextChanged)
{
decimal precio;
if (decimal.TryParse(txtPrecioBase.Text, out precio))
{
precio = precio * (1 + IVATxt.Value / 100);
ignoreTextChanged = true;
txtPrecioTotal.Text = precio.ToString("##.##");
ignoreTextChanged = false;
}
}
}
ignoreTextChanged is a bool that prevents recursive calculations
The problem is that the calculations aren't so precise with two digits.
Assuming IVATxt.Value is 21, If for example we enter 100 (price with tax) to the txtPrecioTotal it calculates a value of 82,64 (price without tax) , but if we enter that value in the txtPrecioBase we get 99,99 (price with tax), when it should be 100.
Is there a way to fix this?
Calculate:
82,64 * 1,21 is 99,9944 (which rounds to 99,99)
82,65 * 1,21 is 100,0065 (which rounds to 100,01)
So, there is no way to represent a price with two decimal places that will result to being 100 when 21% tax is applied. How to solve this highly depends on the application scenario (and also possibly legal requirements). What is the user or the application going to do with the value later on?
Some possible ways to deal with this problem. Note that this is highly dependent on the application scenario and also consider legal requirements on how to do calculations for certain taxes in your customers' region.
For calculated values, allow them having more than two decimal places
When the calculated value is only used for informational purposes and not for further calculations, it can be tolerable to use imprecise value.
In a project I was involved, we allowed vouchers to have a marker whether the calculations will be based on net or gross value. When the voucher is marked as being net based, then the user enters net values. When the voucher is marked as being gross based, then the user enters gross values. The tax calculations are then only applied to the sum total of the voucher (resp. the sum totals of items with the same tax class).
...
Finally solved the problem this way:
Changed decimal precision from 2 to 5 digits in the BD
Set to the entity that contains the product, the price when calculated in one of the two textboxes. This price will have a precision of as much as 5. Continuing the example I posted, a price of 100 with the tax I mentioned before was 82.64 without tax, now it will be 82.64463. If I apply to that value the tax of 21%, I now get the value 100,0000023
Then applying the format F2 or C2 I get those prices with two digits. Continuing with the example, the result of the precedent step will be 100,00, which is the price the user entered.
Thank your for all of your answers.

Convert excel formulas to web based application

I have an excel sheet where there are few formulas applied on each cells and now I want to convert them into a web based application.
Everything seems fine but there are few cells which has formulas which are dependent on each other.
Ex,
Input cells:
Cost: 9790
Allocation: 3%
Non-Allocation: 97%
Cells with formula:
Cell-1: (Cell-3 * Allocation) = (10000 * 3%)
Result: $300
Cell-2: (Non-Allocation * Cell-3) = (10000 * 97%)
Result: $9700
Cell-3: (Cost + Cell-1 - Fee on Damage) = (9790 + 300 - 90)
Result: $10000
So here how will I calculate the dependent cell values as Cell-1 is dependent on Cell-3 and Cell-3 is dependent on Cell-1 ?
EDIT:
Fee On Damage: (Total Fee * Allocation)
And Total Fee: calculated based on Cell-3 (Conditions like if Cell-3 <= 10000 then 3000 etc.)
I did not know that excel can to something like this but so be it.
It turns out that this is a simple excercise in school-algebra:
let's give Cell-3 the variable z
and Cell-1 the variable x
then you have the formulas:
x = z*Allocation
and
z = Cost + x - Fee
and so you can plug in the later into the first and get:
x = (Cost + x - Fee) * Allocation
get all x on the left side:
(1-Allocation)*x = (Cost - Fee) * Allocation
and divide
x = (Cost - Fee) * Allocation / (1-Allocation)
no plug into z:
z = Cost - Fee + (Cost - Fee) * Allocation / (1-Allocation)
(you can simplify the last too)
so let's check with Cost = 9790, Fee = 90 and Allocation = 0.03:
x = 9700 * 0.03 / 0.97 = 300
z = 9700 + 300 = 10000
seems right.
Remark
Obvious it's much harder to this this using some kind of auto-convert tool you might want to write in some common programming language like C#, as you'd need to teach your programm how to do basic algebra ;) - but if you only have a few of those cells and just want to translate it you can do it manually.
You didn't tag JavaScript, but here's a client-side JS solution, which could easily be implemented in c# or asp.net:
var Cost = 9790,
Allocation = 0.03,
Fee = 90,
C1 = 0,
C3 = 0,
C1iterations = 100,
C3iterations = 100;
function Cell1() {
if(--C1iterations) {
C1 = Cell3() * Allocation;
}
else {
C1iterations = 100;
}
return C1;
}
function Cell3() {
if(--C3iterations) {
C3 = Cost + Cell1() - Fee;
}
else {
C3iterations = 100;
}
return C3;
}
document.body.innerHTML= 'Cell1: '+Cell1()+'<br>Cell3: '+Cell3();
You can tell Excel to allow circular references, in which case it defaults to 100 iterations. This code duplicates that functionality, so you don't have to work through the algebra.
All variables are global for demonstration purposes, but they could easily be made local by using closures.

get match percentages between two objects by parameters

I want to create a program that will automate a process that i am doing manually today.
I apologize if the solution seems to be easy i just don't want to think about new algorithm specially for my problem because i am sure that someone already thought about it.
My Scenario is this:
I have candidates list that are looking for jobs and I have jobs list.
For each candidate I know the following requirements of the job that he is searching for. like:
Salary
Location of the Job
Company Size (Big / Small)
In the manual process what i do is to match between those candidate's requirements parameters to the job's requirements parameter and "return" the jobs that seems to fit to the candidate (it doesn't have to be a completely match).
Of course i am considering candidate's requirement is "nice to have" or "must have".
I am searching for an algorithm that returns a fit percentage between each candidate to each job.
Can someone please point me to a any name of matching algorithm like this.
Thanks
My advice is to convert every object to a vector in a 3-D space and then find the Euclidean distance between the two vectors (objects).
First, assign salary, location and size to x, y and z axis, respectively.
Then map the properties to [0, 1] interval of the axis.
For example, if your min salary is 1'000, and max salary is 10'000, then you would map:
$ 1'000 -> 0 on the x axis,
$ 10'000 -> to 1 on the x axis.
Mapping locations is hard, but let's say you have a map grid, and you assign a value to each patch of the grid according to geo position - closer ones have similar values. For example, US states provide us with a good example:
New York -> 1.0 on the y axis,
New Jersey -> 0.99 on the y axis,
...
California -> 0.1 on the y axis.
Map company sizes something like:
start-up -> 0.2 on the z axis,
...
multinational -> 1.0 on the z axis.
So, to give an example: John wants a salary of 9.000, wants a job in New York, and wants to work in a start-up company. His vector in 3D space would be [0.82, 1.00, 0.1].
Peter wants a salary of 5.500, wants a job in New Jersey, and wants to work in a really big company - [0.5, 0.99, 0.8]. And at last, Mike wants a salary of 8.000, a job in California, and a start-up too - [0.73, 0.1, 0.1].
According to formula for Euclidean distance in 3D space:
d(a, b) = sqrt((a1-b1)^2 + (a2-b2)^2 + (a3 - b3)^2)
Distance between John and Peter is: d(J, P) = 0.77
Distance between John and Mike is: d(J, M) = 0.90
So the conclusion would be that John and Peter are closer than John and Mike.
One more thing you could do is to bring in some constants to each axis to emphasize the importance of it (location is more important than company size, for example) so in the formula you could do something like:
d(a, b) = sqrt((a1-b1)^2 + (C*a2 - C*b2)^2 + (a3 - b3)^2), where C = 10
similiarity(A,B) = 1 / (1 + (distance(A,B) / unit))
Case where distance is 0:
similarity(A,A)
= 1 / (1 + (distance(A,A) / unit))
= 1 / (1 + (0 / unit))
= 1 / (1 + 0)
= 1.0
~ 100 %
Case where distance is infinite:
similarity(A,Z)
= 1 / (1 + (distance(A,Z) / unit))
= 1 / (1 + (infinity / unit))
= 1 / infinity
= 0.0
~ 0 %
Code:
JobComparison* compare (Job a, Job b)
{
// define units based on measurement
double unit1 = 1000.0;
double unit2 = 100.0;
double unit3 = 10.0;
// calculate distance
double d1 = abs(a.salary - b.salary);
double d2 = distance(a.location, b.location);
double d3 = abs(a.companySize - b.companySize);
// calculate similiarity
double p1 = 1 / (1 + (d1 / unit1));
double p2 = 1 / (1 + (d2 / unit2));
double p3 = 1 / (1 + (d3 / unit3));
return new JobCompare(p1, p2, p3);
}
public class JobCompare
{
public:
double salarySimiliarity;
double locationSimiliarity;
double companySimiliarity;
}
public class Job
{
public:
double salary;
Location location;
double companySize;
}

Truncating in C#

string input = Console.ReadLine();
decimal sum = Convert.ToDecimal(input);
if (sum >= (decimal)500.01)
{
//40% and 8 dollars off shipping costs are taken off total amount
decimal totalprice;
totalprice = (sum - 8) * .60m;
Math.Truncate(totalprice);
Console.WriteLine("Your final cost is:${0:0.00}", totalprice);
Console.Read();
The problem is, when I enter the price 598.88 dollars into my program, I should get 354.52.
The Math:
598.88 - 8 = 590.88. 590.88 * 60% = 354.528
I actually get 354.53 because C# rounds up instead of down.
For example,
If I get an answer like 519.998, I want it to STAY at 519.99.
Another example, if I get an answer like 930.755 I want it to stay at 930.75.
I looked into some answers, but Math.Truncate obviously doesn't work for me and using the *100 / 100 trick didn't work either. Keep in mind I'm a new student, So, if an answer could be noob-safe, that would be nice. Thanks.
The * 100 / 100 works fine, you might have been using it wrong. Try this below:
decimal totalprice = TruncateToTwoDigits((sum - 8) * .60m);
Console.WriteLine("Your final cost is:${0:0.00}", totalprice);
...
private static decimal TruncateToTwoDigits(decimal Value)
{
int adjusted = (int)Math.Truncate(Value * 100m);
return adjusted / 100m;
}
As a side note, Math.Truncate returns the truncated value, it doesn't change the input parameter as your code would imply.
Math.Truncate like all the other Math function returns the value after the function is called. The function doesn't alter your variable. Actually this was not possible with doubles (please see ref parameters). So you need to do:
totalprice = Math.Truncate(totalprice);
please notice that totalprice will have just the integer part so if the value is 45.985 the result is 45 so you need to multiply by 100 and then divide. http://msdn.microsoft.com/en-us/library/7d101hyf.aspx
The rounding up that you get there is because console.Write calls String.Format that will do that. See http://www.csharp-examples.net/string-format-double/ to get your write function call.
Modulo works too. (It's hard to say which is better for safety, readability, and performance.)
Decimal totalprice = (sum - 8m) * 0.60m; // Discount $8.00 and then 40%.
totalprice -= totalprice % 0.01; // Truncate to two decimal places.
Similar question at Truncate Two decimal places without rounding

Categories