I'm fairly new to programming in general. I have read through a raft of posts regarding casting datatype when using the POW function and have tried to take note.
I can't understand why the following isn't working. In the debugger y is getting assigned to 0. I can't understand why this is. I think it is something to do with how I am converting Doubles/Decimals?
public decimal MonthsPayment(){
decimal monthlyint = this.Rate / 12;
int totalpayments = this.Term * 12;
decimal x = monthlyint * this.Amount;
decimal s1 = (1+monthlyint);
decimal s2 = (-totalpayments);
decimal y = (decimal)(Math.Pow((double)s1,(double)s2));
decimal Repayment = x /(1 - y);
return Repayment;
Related
I can't show the double values in C#
Math.DivRem(double money, int number, out int remain);
Math.DivRem Method is only compatible with Int64 and Int32, which means you can't use double, float or any numeric data type with decimals.
Depending on what you are looking for, this is two other methods of getting the quotient and remainder of your doubles:
double a = 20.1;
double b = 4.93;
double remainder = a % b; // Returns 0.380...
double quotient = a / b; // Returns 4,077...
If you want to make your remainder or quotient to an int, just use
int result = Convert.ToInt32(quotient); // Returns 4
Or to make it more efficient
int result = ConvertToInt32(a / b); // Returns 4
EDIT
One workaround to use Math.DivRem with "doubles", are by multiplying both sides with a constant, for example 100 000. After getting the remainder, divide it with the constant.
double money = 5.515;
int number = 2;
int constant = 100000;
Math.DivRem(Convert.ToInt32(money * constant), number * constant, out int remain);
// Divides 551500 with 200000
// remain = 151500
// For double
double d = (double) remain / (double) constant; // Return 1,515
// For int
remain /= constant; // Return 1
DOUBLE EDIT
Use this instead of Math.DivRem() when dealing with doubles and ints combined.
double money = 12.15;
int number = 5;
double remainder = money % number; // Return 2,15...
int remainder = (int)(money % number); // Return 2
This question already has answers here:
Why is this simple calculation of two doubles inaccurate? [duplicate]
(2 answers)
Closed 2 years ago.
I was coding when I found it:
double v = 555.55;
int v2 = (int)Math.Floor(v / 100.0);
double v3 = v2 * 100;
double v4 = v - v2 * 100;
double v5 = v - v3;
At the end, the code says:
v = 555.55
v2 = 5
v3 = 500
v4 = 55.549999999999955
v5 = 55.549999999999955
So, why is that happening and how could I fix it?
Well, fractional part of double v = 555.55 (which is double v = 0.55) is a periodic binary fraction which
can't be represented exactly. What options do we have? Let's make a smallest possible change of 555.55
(i.e. we change the last bit of 555.55):
double v = 555.55;
int v2 = (int)Math.Floor(v / 100.0);
double v4 = v - v2 * 100;
// Here we increment the last bit of v (minimum possible change)
byte[] bytes = BitConverter.GetBytes(v);
bytes[0] = (byte) (bytes[0] + 1);
double d = BitConverter.ToDouble(bytes);
double dv = d - v2 * 100;
string result = string.Join(Environment.NewLine,
$" v = {v:R}",
$"v4 = {v4:R}",
$" v' = {d:R}",
$"v4' = {dv:R}");
Console.Write(result);
Outcome:
v = 555.55
v4 = 55.549999999999955
v' = 555.5500000000001
v4' = 55.55000000000007
So the only options we have either 55.549999999999955 or 55.55000000000007; please, note, that 55.549999999999955 is a better one (4.5e-14 < 7.0e-14).
How to fix it? As you can see, floating point arithmetics brings rounding error; often we can change double into decimal which is standard practice if 555.55 is some kind of currency value (say, dollars and cents):
decimal v = 555.55m;
int v2 = (int)Math.Floor(v / 100m);
decimal v3 = v2 * 100;
decimal v4 = v - v2 * 100;
decimal v5 = v - v3;
string result = string.Join(Environment.NewLine,
$" v = {v}",
$"v2 = {v2}",
$"v3 = {v3}",
$"v4 = {v4}",
$"v5 = {v5}");
Console.Write(result);
Outcome:
v = 555.55
v2 = 5
v3 = 500
v4 = 55.55
v5 = 55.55
Well, by turning a double into an int, you are going to lose information, because an int is a whole number, so everything after the decimal point gets cut away.
The code below somewhat works but not rounding correctly, my goal is if a value = 1.5 round down, if 1.51 round up.
Thanks
if (!String.IsNullOrEmpty(tbSnp_Uld.Text) && !string.IsNullOrEmpty(cbSnp_Uld.Text))
{
double d_tbSnp_Uld = Convert.ToDouble(tbSnp_Uld.Text);
double d_cbSnp_Uld = Convert.ToDouble(cbSnp_Uld.Text);
double result1 = Math.Ceiling(d_tbSnp_Uld / d_cbSnp_Uld);
double d = 0;
int floored = (int)Math.Floor(d);
int ceiled = (int)Math.Ceiling(d);
double epsilon = 0;
int lessThan = floored - Convert.ToInt32(Math.Abs(d - floored) < epsilon);
int moreThan = ceiled + Convert.ToInt32(Math.Abs(d - ceiled) < epsilon);
tbTrailer_Needed.Text = result1.ToString();
}
Here raw sample of what you trying to accomplish. Wrote this by using TDD apporach: add tests -> make it pass.
So feel free to refactor it
public static int CustomRound(double value)
{
int sign = Math.Sign(value);
double absValue = Math.Abs(value);
int absResult = (int)Math.Round(absValue - 0.01, 0, MidpointRounding.AwayFromZero);
return absResult * sign;
}
I would like to round my answer 1 decimal place. for example: 6.7, 7.3, etc.
But when I use Math.round, the answer always come up with no decimal places. For example: 6, 7
Here is the code that I used:
int [] nbOfNumber = new int[ratingListBox.Items.Count];
int sumInt = 0;
double averagesDoubles;
for (int g = 0; g < nbOfNumber.Length; g++)
{
nbOfNumber[g] = int.Parse(ratingListBox.Items[g].Text);
}
for (int h = 0; h < nbOfNumber.Length; h++)
{
sumInt += nbOfNumber[h];
}
averagesDoubles = (sumInt / ratingListBox.Items.Count);
averagesDoubles = Math.Round(averagesDoubles, 2);
averageRatingTextBox.Text = averagesDoubles.ToString();
You're dividing by an int, it wil give an int as result. (which makes 13 / 7 = 1)
Try casting it to a floating point first:
averagesDoubles = (sumInt / (double)ratingListBox.Items.Count);
The averagesDoubles = Math.Round(averagesDoubles, 2); is reponsible for rounding the double value. It will round, 5.976 to 5.98, but this doesn't affect the presentation of the value.
The ToString() is responsible for the presentation of decimals.
Try :
averagesDoubles.ToString("0.0");
Do verify that averagesDoubles is either double or decimal as per the definition of Math.Round and combine these two lines :
averagesDoubles = (sumInt / ratingListBox.Items.Count);
averagesDoubles = Math.Round(averagesDoubles, 2);
TO :
averagesDoubles = Math.Round((sumInt / ratingListBox.Items.Count),2);
2 in the above case represents the number of decimals you want to round upto. Check the link above for more reference.
int division will always ignore fraction
(sumInt / ratingListBox.Items.Count);
here sumint is int and ratingListBox.Items.Count is also int , so divison never results in fraction
to get the value in fraction , you need to datatype like float and type cast the sumInt and count to float and double and then use divison
var val= Math.Ceiling(100.10m);
result 101
What's the proper way to do "integer" divisions with decimal types in C# ?
I.e.
decimal a = 130, b = 60;
decimal res = a / b; //need to get 2.0, not 2.6666
In this case I'd use the Floor function.
decimal res = Math.Floor(a / b);
decimal a = 130, b = 60;
decimal res = Math.Floor(a/b);
You can use Decimal.Truncate(a / b);
Decimal.Truncate() "rounds" towards zero, and is thus like Math.Floor() for positive numbers and Math.Ceiling() for negative numbers.