Why double becomes int? [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
When I run this code, I get output "System.Double, 3"
namespace NewApp
{
class Program
{
static void Main(string[] args)
{
double a = 1.0;
double b = 2.7;
a = Math.Round(b);
Console.WriteLine(a.GetType() + ", " + a);
}
}
}
Why I see "3", if a is double variable and I supposed to see "3.0"?

It is still double. You are facing with 3 instead of 3.0 because of the way Console.write works. Use this as example
Console.WriteLine(DoubleConverter.ToExactString(a))

That's because you are saying a is Math.Round(b);
Meaning a will be 3
double a = 1.0; // a -> 1.0
double b = 2.7; // b -> 2.7
a = Math.Round(b); // a = 2.7 "rounded up" -> a = 3
Console.WriteLine(a.GetType() + ", " + a); // a is a double and
// the value is 3 (check previous line)
Edit:
About the decimals, if you round up, you get no decimals, so its 3 instead of 3.0 I believe
Math.Round(); returns a value with no decimals

Related

How to set a while loop? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to set my code for counting math games, all math operations are fine except for division. I don't know how to set the while loop correctly. There is a problem with division, such that I would like the division to be residual, so I came up with one method which is given below. It is all in WPF Application. I would like to count only single-digit numbers.
Random number = new Random();
int maxValue = 10;
int total = 0;
int firstNumber = number.Next(1, maxValue);
int secondNumber = number.Next(1, firstNumber);
while (firstNumber % secondNumber != 0);
{
secondNumber++;
}
total = firstNumber / secondNumber;
Why does it still show me the values ​​that have a residual division?
Thank you for any advice
The semi colon at the end of line:
while (firstNumber % secondNumber != 0);
...ends the while loop. The code in the remaining block is executed without any condition (as it in fact is a anonymous block):
{
secondNumber++;
}

Rounding a double not correct output [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to round a number and the expected output isn't correct. Here's what I have tried:
var percent = Math.Round(1.13451, 0);
That above return's 1, but I want it to return 1.13 if the 3rd number is less than 5. If it's >= 5 (the third number) I want to get something like 1.135. I am sure it's something simple I am missing, but not sure.
It appears you are causing confusion because you're using the term "rounding" to describe an operation that is not actually rounding.
I've read your description again, and I can see that you're trying to truncate your values into the highest discrete increment of 0.005 that does not exceed the value.
You can do this as follows:
var percent = Math.Floor(200.0 * x) / 200.0;
Or, if you want it to be more obvious what's happening, this is essentially the same thing:
var increment = 0.005
var percent = Math.Floor(x / increment) * increment;
you have to write it with the amount of decimals you want. var percent = Math.Round(1.13451, 2);
Updated: I think this is the easiest way to do it.
static void Main(string[] args)
{
var percent = 1.13551;
char[] percent1 = percent.ToString().ToCharArray();
if (percent1[4] <= 5)
{
percent = Math.Round(percent, 3);
}
else
{
percent = Math.Round(percent, 2);
}
Console.WriteLine(percent);
Console.Read();
}
Use:
var percent = Math.Round(1.13451, 2);
Inside an if statement

Lambda and Math operation [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I need some help with this:
I have a lambda function that looks like this:
Func<double, double> f = (x) => (Math.Pow(2 * x, 3) + Math.Pow(x, 2)) + ((5 * x) + 7);
It then returns 555 when it was supposed to return 171.
So I found out that the order should be:
7 + (5 * x)
(x^2) + (2x^3)
And then add those results together, is there a way to change the order in which it's calculated because parentheses don't seem to work here
Parenthesis do work, if you set them in the correct position.
If you want f(4) to return 171 instead of 555 the correct expression is
2 * Math.Pow(x,3) + Math.Pow(x,2) + x * 5 + 7
The way you wrote it it does (2*x)^3 instead of 2*(x^3)
To solve the problem you need to extract the constant 2 from Math.Pow()
Func<double, double> f = (x) => 2*Math.Pow(x, 3) + Math.Pow(x, 2) + (5 * x) + 7;

C SHARP How to exclude certain number calculation [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
How can I block the Divide Zero by Zero calculation. My calculator is allowed to do a | + | - | * | calculation but I don't want it to execute a zero by zero calculation with dividing.
thanks!
protected void btnButton1_Click(object sender, EventArgs e)
{
double get1;
double get2;
double answer;
get1 = Convert.ToDouble(txtGetal1.Text);
get2 = Convert.ToDouble(txtGetal2.Text);
answer = get1 / get2;
txtUitkomst.Text = Convert.ToString(answer);
}
I receive a NaN
You can check before dividing and return 0 if denominator is zero.
protected void btnButton1_Click(object sender, EventArgs e)
{
double get1;
double get2;
double answer;
get1 = Convert.ToDouble(txtGetal1.Text);
get2 = Convert.ToDouble(txtGetal2.Text);
answer = (get2==0) ?0 : get1 / get2; // check for 0 and return 0
txtUitkomst.Text = Convert.ToString(answer);
}
To my understanding, you would have to check in input. If the user tries to enter a value of zero as the second argument of the division, your program has to reject the input and prompt the user to enter a nonzero value.

Converting From String to INT saves another value [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have one function in which i take the numbers as string and when i convert the numbers to int they are saved with some other value.
Why is that Please help.
private string DoTheMath()
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int n1 = Convert.ToInt32(s1[0]);
int k1 = Convert.ToInt32(s1[1]);
}
when i input 49 51
int n1 gets value 31
and int k1 gets value 33
Since you are parsing strings to ints, you probably want Int32.Parse:
private string DoTheMath()
{
string s = Console.ReadLine();
string[] s1 = s.Split(' ');
int n1 = Int32.Parse(s1[0]);
int k1 = Int32.Parse(s1[1]);
}

Categories

Resources