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.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I need a method to return the firsts non zero numbers from a double in the following way: Any number >= 1 or == 0 will return the same; All the rest will return as per the following examples:
(Please note that I am using double because the potential imprecision is irrelevant in the use case whereas saving memory is relevant).
double NumberA = 123.2; // Returns 123.2
double NumberB = 1.2; // Returns 1.2
double NumberC = 0.000034; // Returns 3.4
double NumberD = 0.3; // Returns 3.0
double NumberE = -0.00000087; // Returns -8.7
One option would be to iteratively multiply by 10 until you get a number greater than 1:
public double RemoveLeadingZeros(double num)
{
if (num == 0) return 0;
while(Math.Abs(num) < 1) { num *= 10};
return num;
}
a more direct, but less intuitive, way using logarithms:
public double RemoveLeadingZeros(double num)
{
if (num == 0) return 0;
if (Math.Abs(num) < 1) {
double pow = Math.Floor(Math.Log10(num));
double scale = Math.Pow(10, -pow);
num = num * scale;
}
return num;
}
it's the same idea, but multiplying by a power of 10 rather then multiplying several times.
Note that double arithmetic is not always precise; you may end up with something like 3.40000000001 or 3.3999999999. If you want consistent decimal representation then you can use decimal instead, or string manipulation.
I would start with converting to a string. Something like this:
string doubleString = NumberC.ToString();
I don't know exactly what this will output, you will have to check. But if for example it is "0.000034" you can easily manipulate the string to meet your needs.
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
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
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am trying to use the Math.Floor method on 2 user-input numbers but whenever I try to use the Math.Floor with my input3 and input3a it just doesn't work. I've seen examples for already set numbers such as in an array but not of numbers that the user inputs. Any help/clarification would be awesome.
static void Minimum()
{
Console.WriteLine("\n Enter two numbers and I shall determine the Minimum:\n");
Console.Write("> ");
Console.Write("\n> ");
// Read and parse input
string input3 = Console.ReadLine();
double d_input3 = Double.Parse(input3.Trim());
string input3a = Console.ReadLine();
double d_input3a = Double.Parse(input3a.Trim());
// Determine minimum of numbers
Console.WriteLine("\nThe Number {0} and {1}.\n", d_input3, d_input3a);
}
If think you don't need Math.Floor to determine a minimum :
Returns the largest integer less than or equal to the specified
double-precision floating-point number.
If you need to determine the minimum of both numbers, use Math.Min instead
Console.WriteLine("\nThe minimum number between {0} and {1} is {2}.\n", d_input3, d_input3a, Math.Min(d_input3, d_input3a));
The Math.Floor function takes one parameter and returns the value rounded down:
d_input3 = Math.Floor(d_input3);
d_input3a = Math.Floor(d_input3a);
The Math.Floor method is however not used to determine the lower value of two values. For that you would use the Math.Min function instead:
double lowest = Math.Min(d_input3, d_input3a);
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
I have a quiz that have many questions and 5 possibilities of answers.
Lets take one question,it has follow answers:
148 - Good
5 - N/A
268 - Great
5 - Regular
11 - Bad
These are numbers collected directly from database.Now i need to show it as percentage.i.E:
Great - 45%
Good - 40
[..]
and so on
Any ideas?
int na = 5;
int good = 148;
int great = 268;
int regular = 5;
int bad = 11;
int sum = na + good + great + regular + bad;
naPercent = getPercent(na,sum);
float getPercent(int value, int sum)
{
return (value*100.0)/sum;
}
This is not a programming question, it is a math question. The percentage of each item is equal to the number of that item divided by the total number. In your example, the total number is 148+5+268+5+11 = 437. Great = 268 / 437 = 61.327%
total count for the answer / total count for all answers to this question combined * 100