Get Maximum Amount Multiple of 50, without cents - c#

I'm trying to fill a textbox with an amount that can be divided by 50 and with no cents.
Examples:
Amount -> 52353.85
Should Display -> 52350
Amount-> 1229.68
Should Display-> 1200
How can I modify the value?
Thanks in advance.

int newValue = (int)(oldValue / 50) * 50;
add 25 to the old value if you want the number could round up
int newValue = (int)((oldValue + 25) / 50) * 50;

This gives the properly rounded result:
return Math.Round(x / 50.0) * 50;

return (int)x - ((int)x % 50);

Related

Obtain the interval of the thousandth of a number c#

I'm trying to get from a number the interval where this number is.
For example my number is 10:
By tens: [1-100]
My number is 110:
By tens [101-200]
Thank #Juharr !
double n = 150;
double test = Math.Floor(n / 100) * 100 + 1;
double test2 = Math.Floor(n / 100) * 100 + 100;

Cant calculate percentage difference between two numbers

I am trying to show the percentage after a number is subtracted.
Example:
Cost of work = £165.00
Workers charge = £42.00
Left Over = %
What is the percentage of Cost left after the worker has got his cut.
My Code output is showing 0
int number = 0, number1, result = 0;
if (Int32.TryParse(SelectedQuoteForEditing.JobPrice, out number) && Int32.TryParse(Rate.Content.ToString().ToString(), out number1))
{result = number - number1;}
JobPercentage.Content = result.ToString();
The simple formula is PART / MAX * 100
In your fault It should look like this:
double costOfWork = 165;
double workersCharge = 42;
double left = Math.Round((costOfWork - workersCharge) / costOfWork * 100, 2);
It calculates the remaining cost using costOfWork - workersCharge.
Then it calculates how many percents the remaining cost is.
It's getting rounded to two digits.
I ran across the issue in C#- FileComparison
double size = (LengthOfTxt-LengthOfDocx) / LengthOfDocx / 100;

showing a percentage number for two variables in c# [duplicate]

This question already has answers here:
C# is rounding down divisions by itself
(10 answers)
Closed 6 years ago.
I have two variables, I want to showing as percentage, when I calculate them with operator the result is 0 why?
please help me. Thanks
this is my source
int count = (from a in dc.jawabans
where a.q14 == "5 : Sangat Baik/ Sangat Puas"
select a).Count();
TextBox1.Text = count.ToString();
int total = (from b in dc.jawabans
where b.q14 != ""
select b).Count();
TextBox2.Text = total.ToString();
int persen = (count / total) * 100;
TextBox3.Text = persen.ToString();
This is the result
count is int, total is int too. In C# when int divided by int the result is int. The solution is to cast one variable as double.
int persen = (int)((double)count / total * 100);
Write it like this:
decimal persen = (count / (decimal)total) * 100;
After that you can round it if you want:
TextBox3.Text = Math.Round(persen, 2).ToString();
Division of 2 integers is an integer, so you should specified that one of them is decimal.
Because you dividing two integers, so the result will be integer as well. You can set count and total as double , then you will get correct result.
This is because the sum you are doing is with ints, so the value is rounded to the nearest whole number - for example if count is 20, and total is 100
int persen = (count / total) * 100;
is the same as doing
int persen = (count / total); //this = 0 as it would evaluate to 0.2 => 0
persen = persen * 100; //still 0
Whereas
int persen = ((double)count / (double)total) * 100;
//This would be 20, as count and total are both cast to a double - it also works if you only cast one of them
decimal persen = (count / (decimal)total) * 100; //count 20, total 100, so person will be 0 if it is int in your code
If you devide int by int, it will give you int not double.
So either convert count or total as decimal or double according to your requirement.

Get value between a range of two values from percentage

Let's say I have a range of two values:
5...........98
and let's assume the user position's the slider at value 40
Now I want to get the value from another range of values at the exact percentage position as from range 1
let's say the second range of values are 10.........80
int nRange1 = 98 - 5;
int nRange2 = 80 - 10;
int nValue1 = 40;
int nPercentOnRange1 = ((nValue1 - 5) / nRange1)*100;
Now I have to get the value from Range2 at the exact percentage as nPercentOnRange1, but I don't know how
First need to find % from first range and apply that % to new range.
Here is what I will do:
Range1(A to B) Selected value: c
Range2(E to F)
Range1 % = (C-A) / (B-A) * 100
Range 2 corresponding value = ((F - E) * (Range 1 %) / 100) + E
C#:
int Range1Min = 5, Range1Max=90, Range1SelectedValue = 40;
int Range2Min = 6, Range2Max=80;
decimal range1Percent = (Range1SelectedValue-Range1Min ) / (Range1Max-Range1Min) * 100.0
decimal range2NewValue = (Range2Max - Range2Min) * range1Percent / 100 + Range2Min;
Watch out for
int nPercentOnRange1 = ((nValue1 - 5)/ nRange1) * 100;
ending up as zero since nValue1 and nRange1 are integers. This might be better:
int nPercentOnRange1 = ((nValue1 - 5) * 100 / nRange1);
Then you can do
int nValue2 = 10 + nPercentOnRange1*nRange2/100;
The value you need is
x = 10 + nRange2 * nPercentOnRange1 / 100.0
Let me explain why. You need a number x such that
((x - 10) / nRange2) * 100.0 = nPercentOnRange1
Therefore, just solve for x.
((x - 10) / nRange2) * 100.0 = nPercentOnRange1 =>
((x - 10) / nRange2) = nPercentOnRange1 / 100.0 =>
x - 10 = nRange2 * nPercentOnRange1 / 100.0 =>
x = 10 + nRange2 * nPercentOnRange1 / 100.0
And note that this actually makes intuitive sense. We're saying take the percentage, scale that into the length of the second range (that's what nRange2 * nPercentOnRange1 / 100.0) is doing and then add that to the lower bound of the second range. Basically we are saying step nPercentOnRange1 percent into the second range. That's exactly what the formula is expressing.
Perhaps this will work:
nValue2 = nPercentage1 * nRange2 / 100 + 10

Translating Excel formula to c# (annuity)

I have a excel formula I am trying to "translate" into c# code.
It is used to calculate an "annuity rate" over time (for example 20 years).
=(((1+E26/100)^D28*((1+E26/100)-1))/((1+E26/100)^D28-1))*100
D28 = 20 (years)
E26 = 5,00 (the rate in percent)
the ^ stands for exponent in Excel
As a result with these numbers I expect 8,02% per annum.
I tried several approaches using Math.Pow but wasn't successful.
Here is my first approach which gives me a result of 5 somehow.
double usagePanels = 20.0
double rate = 5.0
annPanels = (Math.Pow((1 + rate / 100), usagePanels) *
((1 + rate / 100) - 1) /
Math.Pow(1+rate/100, (usagePanels-1))) * 100;
Thank you.
Try:
double usagePanels = 20.0
double rate = 5.0
double annPanels = (Math.Pow((1 + rate / 100), usagePanels) *
(rate / 100.0)) /
Math.Pow(1+rate/100, usagePanels)-1)) * 100;
You've got the closing bracket, between usagePanels and the -1, wrong...
(I found this by breaking the formula apart in Excel and in C# and comparing each part.)
EDIT: Another handy tip for comparing Excel to C# is to give the cells in Excel a name (via the Named Range feature) that way the Excel formula can be made to look closer to variable names...
This should do the trick:
double rate = 5;
double years = 20;
double annunity = Math.Pow(1 + rate / 100, years) * (rate / 100) / Math.Pow(1 + rate / 100, years - 1) * 100;
For clarity, the working result is
double usagePanels = 20.0
double rate = 5.0
annPanels = (Math.Pow((1 + rate / 100), usagePanels) *
((1 + rate / 100) - 1) /
(Math.Pow(1+rate/100, (usagePanels))-1)) * 100;
Thanks to Jason Allen and Grhm who basically figured it out and gave great advice.

Categories