Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I have two dates.
ReleaseDates = "11/2/2016";
LiveDate = "11/02/2016";
In the above two dates are same. But in my below coding it returns FALSE.
if (ReleaseDates.Contains(LiveDate.TrimStart(new Char[] { '0' })))
{
}
Your code does not work because TrimStart removes characters at the start of the string. It looks like LiveDate has no zeros at the beginning; the character '0' that you want to trim is at index 3, preceded by other characters. That is why it is not getting trimmed.
Comparing strings representing dates is an error-prone strategy. You would be better off parsing both strings, and comparing the results as DateTime objects.
In general, you should keep dates as objects designed for date representation - for example, DateTime or DateTimeOffset. This would let you customize date representation for display purposes, and avoid errors when date format changes from mm/dd to dd/mm.
This is because you are not comparing dates, you are comparing two strings that are not the same. Your best shot here is parsing first and then comparing.
DateTime releaseDate = DateTime.Parse(ReleaseDates);
DateTime liveDate = DateTime.Parse(LiveDate);
if (releaseDate == liveDate) // This should be true.
{
// Do stuff.
}
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 4 months ago.
Improve this question
I need to convert a string to decimal with specific format in C#. This string can be in different formats. For example it can be: 20 or 20.5.
I need it to convert to xx.xxx. Is there is a method to do this?
C# decimal values are binary. They do not have a human-readable format and can NEVER have a human-readable format. Anything you see otherwise is a convenience provided by the debugger or other tooling. If you need a decimal formatted in a certain way, what you really need is a string.
That said, the decimal type is a good intermediary to be sure you get the correct desired string output: first Parse() the original string value to a decimal, then convert from a decimal to the final formatted string result using the decimal's ToString() method.
Finally, it's important to understand cultural and internationalization issues mean converting between strings and true numeric values is far more error-prone and slow than we'd like to believe. It's something to avoid. Therefore the best strategy is usually parsing a value into an unformatted decimal as quickly as possible, and then keeping it there as long as possible — until the last possible moment before you need to format it for output.
You can specify the format like the below:
string t = "2020.5";
var d = decimal.Parse(t).ToString("00.000");
If your string contains any special character you need to sanitize it before you parse it. E.g. if the string contains space you can replace it with empty char like this:
t = t.Replace(" ", "");
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
In c# when adding two decimals the program will automatically get rid of the number 0 after the decimal places.
For example adding 0.50 to 1.20 will produce the answer of 1.7 and this is annoying because i need to display this answer in terms of money.
Is there a way to prevent this?
If you want to display your Decimal with two decimal places, please use :
myDecimal.ToString("N2");
You may want to take a look at Standard Numeric Format Strings for more information.
decimal d = 0.50m;
decimal d1 = 1.20m;
Console.Write(d+d1);
Please find this Post
I'm not sure about if you mean this, but you can try the toString() method in currency format this way:
double number = 1.2;
string numberCurrency = number.ToString("C");
Console.WriteLine(numberCurrency); //this prints "1.20"
I recommend you to read this https://msdn.microsoft.com/es-es/library/kfsatb94(v=vs.110).aspx
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
How can I build a regular expression that will check this code:
ABC00000
The ABC is fixed and the 0 is a place holder for numbers.
The maximum length of numbers is 5.
Use this Regex:
ABC(\d{5})
The capturing group will also help you to retrieve the number after 'ABC', if you need it.
A non-regex way:
testString = "ABC00000";
if (testString.StartsWith("ABC") && int.TryParse(testString.Substring(3), 0))
{
}
The above code basically checks if the first three characters are 'ABC' and the last 5 characters are numeric. The int.TryParse() function returns if the number is parse-able from the string, i.e., if it is a number.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i wanna ask. How do i put "," sign on the number whenever the number length is more than 3, then add 1 "," sign.
for example:
I have a number "100000000", and i want computer display it as "100.000.000,00", how do i do that?
Here is the image:
In picture above, shown that the SubTotal is "10000", i want computer display it as "10.000,00" and the Total beside SubTotal is "10000000", i want computer display it as "10.000.000,00".
My question is: how do i do that?
Thanks
A lot of it depends on the control(s) you are using. If you're using plain text boxes you can just set the format when setting the Text value:
txtbox1.Text = total.ToString("N2"); // numeric with separators and 2 decimal places
Other third-party controls let you choose the format with a property such as NumberFormat. Grid controls usually set the format on a column rather than an individual cell.
You should use
amount.ToString("N");
If you're doing it programmatically:
int myNumber = 10000000;
string output = String.Format("{0:n2}", myNumber);
or
int myNumber = 10000000;
string output = myNumber.ToString("n2");
The number after n is the number of decimal places (which can be 0 if you want).
Or you might need to set a format string of a user control to "n2" (without quotes) depending on how you are displaying the numbers.
Have a look at standard numeric format strings and custom numeric format strings on MSDN.
You should use the numeric format specifier to achieve what you want:
number.ToString("N", CultureInfo.InvariantCulture);
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 8 years ago.
Improve this question
I have a problem that I'm not able to solve.
I'm getting a value from a Database. This value in the database is saved as a nvarchar.
I need to understand the type of the value.
For example:
12.12.2012 -> DateTime
15:00 -> Time
67.45 -> Double
For undesrtand the type I use the TryParse method.
Example for understand if it is a DateTime I execute this code:
DateTime valueConverted;
if(DateTime.TryParse(input.ToString(), out valueConverted))
{
// IS DATETIME
}
The problem is that if the value got from the DataBase is:
0900
009000
I would like the recognize it as a String. The problem is that the conversion to a double has success and then the value is converted to a double with value = 900.
How can I recognize that the value like 0900, 00060 are not double but String?
The simplest would probably be to match against ^0[0-9]+$.
Realize though this is a non-trivial problem and you're bound to find edge cases you didn't expect. I recommend changing the data model if you are at all able, and either store different types in different columns or have a separate column that denotes the data type stored. Storing everything as a string is generally a Bad Idea.
This seems massively oversimplified, but based on your description it will do the trick:
if (!string.IsNullOrEmpty(input) &&
input[0] == '0' &&
input.All(c => char.IsDigit(c)))
{
// We have a string.
}
you can used regex-match
create all type of datatype format string and match with regex.
http://www.dotnetperls.com/regex-match
if (input.StartsWith("0"))
{
// string
}
else
{
// double
var number = double.Parse(input);
}