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;
Related
I am trying to write a code in C# that prints out the total of dollars and cents I have. I have the first part done, but the end of the code won't show how much cents there is when I run the whole code, it only shows a whole number (such as $1), when I need the code to show something like $1.56. What am I missing in the program in order to show this?
Console.WriteLine("Enter number of quarters: ");
int quarters = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of dimes: ");
int dimes = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of nickels: ");
int nickels = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter number of pennies: ");
int pennies = Convert.ToInt32(Console.ReadLine());
double dollars = (int)(((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100);
Console.WriteLine("Your total is $" + dollars);
When I typed in 4 quarters, 0 dimes, 0 nickels, and 1 penny, it shows the result as $1 not $1.01
You are forcing the result as int, so the decimals are cut
double dollars = (int)(((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100);
Do not force to int, on the contrary, force to double
double dollars = ((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100;
But, since you are handling money, I would recommend to handle everything with decimal
decimal dollars = ((quarters * 0.25) + (dimes * 0.10) + (nickels * 0.05) + (pennies * 0.01)) % 1 * 100;
Better explained in dotnet perls
Decimal accurately stores numeric data. The .NET Framework offers this
type for programs where rounding errors are harmful. Decimal stores
large and small numbers with many digits after the decimal place.
EDIT
When I put in decimal dollars instead of double dollars, I then get
the error, "Cannot implicitly convert type 'double' to 'decimal'. An
explicit conversion exists (are you missing a cast?)
It is becuse you need all the operation of the same type, you can do
int quarters =1;
int dimes = 1;
int nickels = 1;
int pennies = 1;
decimal dollars = ((quarters * 0.25m) + (dimes * 0.10m) + (nickels * 0.05m) + (pennies * 0.01m)) % 1 * 100;
Console.WriteLine("Your total is $" + dollars);
Note the use of a m after the number, this is to tell the compiler that the type is decimal or Money, not double.
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;
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.
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
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);