How to round up the number in c# [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
How can I round up values like this:
1.001 => 2
3.3 => 4
Means if the number has fractional part than i want the smallest integer number greater than the number ?
I used Math.Ceiling() but is not helping. How can i do this ?

Math.Ceiling will work. can you tell what its not working ? in term of any errors or returned result.
var returnVal=Math.Ceiling(yourValue);

Use Math.Ceiling() method.
Returns the smallest integer greater than or equal to the specified
number.
var i = Math.Ceiling(1.001);
var j = Math.Ceiling(3.3);
Console.WriteLine(i);
Console.WriteLine(j);
Output:
2
4

Math.Ceiling(value);
Should work.

double x;
x = Math.Ceiling(5.2) ;//Result; 6
x = Math.Ceiling(5.7) ;//Result; 6
x = Math.Ceiling(-5.2) ;//Result;-5
x = Math.Ceiling(-5.7) ;//Result;-5
This is a simple example. How can't you use it? Maybe you miss to assign a variable to
Math.Ceiling();

Related

How to remove negative sign from LINQ output [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
i have following linq used in my application
var FinalSubExpired = subExpired.Where(e => (DateTime.Now - Convert.ToDateTime(e.AreasOfLawTillDate)).TotalDays <= 30 ).ToList();
which returns the total days with negative values, i need to remove that negative sign from that total days. how can i do that by modifying this linq?
Please help.
var FinalSubExpired = subExpired.Where(e => Math.Abs((DateTime.Now - Convert.ToDateTime(e.AreasOfLawTillDate)).TotalDays) <= 30).ToList();
that should do the trick
try and use Math.Abs inside your Linq query, as:
var FinalSubExpired = subExpired.Where(e => (Math.Abs(DateTime.Now - Convert.ToDateTime(e.AreasOfLawTillDate)).TotalDays) <= 30 ).ToList();

C# reset many integer variables quicker [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is there a way to reset these variables quicker like at the time of declaration?
Declaration:
int numa1, numa2, numa3, numd1, numd2, numd3;
Reset afterwards:
numa1 = 0;
.
.
.
numd3 = 0;
Because I will change these variables, but then I need to reset them as 0, OK?
Replying the comments below:
Sorry, I didn't change "Reset afterward" to "Initializes afterward". Someone else did that.
Sorry, but you cannot change the values of the variables like:
numa1, numa2, numa3, numd1, numd2, numd3 = 0;
I tried and I received Error 1, 2, 4.
By quicker if you mean faster, I donot think there is any better alternative.
If you mean reducing some lines,
You can choose either
int numa1=0, numa2=0, numa3=0, numd1=0, numd2=0, numd3 = 0;
or
int numa1, numa2, numa3, numd1, numd2, numd3 = 0;
numa1= numa2= numa3= numd1= numd2= numd3;
I guess that you should have used two arrays, I never had a function that used so many variables with such names (sequential numbers).
If you'd use arrays, Your code will look like this :
int[] numa = new [] {0,0,0,0};
int[] numd = new [] {0,0,0,0};
But this is up to you.
For more information about arrays

Convert int(round(time.time())) to C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am trying to convert this python instruction to C#
int(round(time.time()))
But I cannot figure out what it does exactly.
You need to use UtcNow as opposed to Now or else you will get an answer offset by your timezone.
TimeSpan t = (DateTime.UtcNow - new DateTime(1970, 1, 1));
int timestamp = (int) t.TotalSeconds;
time.time() will return the current time as a float which represents seconds since 1/1/1970, round() will round that float to the nearest integer value, and int() will convert the value to the integer type.
For example:
>>> time.time()
1351702579.645324
>>> round(time.time())
1351702580.0
>>> int(round(time.time()))
1351702580

Convert double array to list [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
txt file that contains several columns of numbers, every column is saved in a double array, what I want to do is get the average of a specific column, but to do that I have to convert the array to a list and then began to calculate. I have this code so far:
List<double> 1 = new List<double>(NumSepaERG);
List<double> 12 = NumSepaERG.ToList();
But i get and error of Invalid Expression term double
Variable names can't start with a numeric character. Change to something like:
List<double> list1 = new List<double>(NumSepaERG);
but you can compute an average using Linq without converting to a list:
double average = NumSepaERG.Average();
If NumSepaERG is a jagged array (an array of arrays), the syntax would be:
double average = NumSepaERG[i].Average();
where i is between 0 and the number of arrays - 1;
I think it's a syntax error, you can't have numbers as variable names. You actually don't even need to make it a list.
double average = NumSepERG.Average();

How to use .NET Regex to express positive numbers with decimal [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
How can use regular express to express the positive amount with most 20 digits in integral and 6 decimals, such as 0.25, 1234566789.123456?
Thank you.
var regexStr = #"^\d{1,20}(\.\d{1,6})?$";
var r = Regex.Match("15", regexStr); // match 15
r = Regex.Match("15.158", regexStr); // match 15.158
r = Regex.Match("-22.9", regexStr); // fail, negative
r = Regex.Match("123456789012345678901.1234567", regexStr); // fail, too long
r = Regex.Match("-123456789012345601.123456", regexStr); // fail, negative
r = Regex.Match("123456789012345601.123456", regexStr); // match 123456789012345601.123456
try this:
^\d{1,20}(.\d{1,6})?$
Try this:
(?<![-\d\.]) \d{1,20} (\.\d{1,6})? \b
Test cases:
0.25, 1234566789.123456, 5.66, -12345678901234567890.1, 12345678901234567890.1, 5
There you go : #"^[0-9]{1,20}(\.[0-9]{1,6})?$"

Categories

Resources