How to use .NET Regex to express positive numbers with decimal [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 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})?$"

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();

How to round up the number in 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.
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();

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

Integer Formatting as thousand separator first, hundred separator afterwards [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 want to format an integer so that it appears first with the 1000's separator (,), but after that with 100's separator (,)
Input:123456789
Output: 12,34,56,789
You can create a NumberFormatInfo that has a NumberGroupSizes array that gives you that format:
NumberFormatInfo info = new NumberFormatInfo();
info.NumberGroupSizes = new int[]{3,2};
Console.WriteLine(123456789.ToString("#,#", info));
Output:
12,34,56,789
This can help you out:
1. the currency depending on the culture,
2. the currency in your wanted format without any cash mark
int iValue = 2879068;
string sValue1 = String.Format("{0:C}", iValue);
string sValue2 = String.Format("{0:#,#.}", iValue);
but in case if you want to have some cash mark, simply do:
{0:$#,#.}//or
{0:#,#.€}
Hope it helps,

Sum of characters in textbox [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'm tasked with building a program that performs math based on letters. Every letter has one value. For example, A=1, B=2 and C=3. The user will enter a series of letters in a text box. If the user enters ABC, the result would be 6.
How do I perform this math?
Here is a picture of the user interface I have built:
var text = "Abc";
text = text.ToUpper();
var sum = 0;
foreach (char c in text)
{
sum += c - 64;
}
sum has your value.

Categories

Resources