Rounding a double not correct output [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 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

Related

Why double becomes int? [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 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

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++;
}

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;

For loop with a mathemtical expression involved [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 4 years ago.
Improve this question
I am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2pi increasing by .1pi, so 20 iterations. The problem is I cannot get my x value to change as the for loop is executing.
public void Cos()
{
double x = 0;
double a = Math.PI * x;
double b = Math.Cos(a);
for (int i = 0; i < 21; i++)
{
Console.WriteLine("Cos({0})pi = {1}", x, b);
x += .1;
}
}
When I print the results to the console, it only remembers the value of cos at x = 0. So I just get 1, 20 times as a result for Cos(.1)pi, Cos(.2)pi, etc...
I am trying to utilize the Math.Cos() function in c# to print a range of of values from 0 to 2PI increasing by .1PI
This sounds like a job for a for loop, where we start with a value of 0, and increment by .1 * PI on each iteration until it reaches 2 * PI.
Since a for loop has an initializer part, a condition part, and an incrementing part, it is the perfect construct. No need for an extra variable that increments from 0 to 20 - we can use the for loop to do our incrementing of x and our testing the exit condition!
public static void Cos()
{
for (double x = 0; x <= Math.PI * 2; x += Math.PI * .1)
{
Console.WriteLine("Cos({0})PI = {1}", x, Math.Cos(x));
}
}

C# loop on fraction [closed]

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 4 years ago.
Improve this question
I need someone to give me an idea on how to go on about this problem.Using a loop to calculate the fraction , There is no common value.I want to get the sum
Eg for fraction :
1 1/5 1/10 1/15 1/20 … 1/290 1/295 1/300
code snippet:-
int sum=0;
for(int i=1;i<=60 ;i++)
{
int sum=1
}
These sort of problems are actually surprisingly non-trivial due to issues with working with floating point, and decimal types for that matter.
Accepting that you want a loop solution for this (a closed form solution for n terms does exist), first note that your series can be written as
1 + 1/5(1 + 1/2 + 1/3 + ... + 1/60)
Then note that a good rule of thumb when working with floating point types is to add the small terms first.
So an algorithm would be of the form
double sum = 0.0;
for (int i = 60; i >= 1; --i){
sum += 1.0 / i;
}
sum = sum / 5 + 1;
Note the 1.0 in the numerator; that's there to defeat integer division.
Reference: Is floating point math broken?
͏Since you asked for a hint:
float sum = 1.0;
for (int i = 5; i <= ??; i += ??) {
sum += 1.0/i;
}
What goes in place of the ??s?
try this code:
double sum=1;
for(int i=5; i<=300; i+=5)
sum += (double) 1 / i;
The value of sum will be 1.93597408259035

Categories

Resources