This question already has answers here:
How can I check if a string is a number?
(25 answers)
Closed 7 years ago.
i'm currently doing a C# WPf application.
May i know how to validate for the string value? Example "0.00" or "-1.00",etc?
Because both of them is return as a string that i retrieve from SAP, is there anyway to check?
Sorry I'm still new to C# , not really familiar with the function that have in the C#.
I assume you want to check if the number that you get back is a negative number?
double number = 0;
if(double.TryParse(myString,out number)){
if (number > 0)
\\Do something
}
Do you always have a double value?
Try
Double.Parse(...)
edit: corrected method
you can use TryParse() as bellow
string s="0.00";
double dvalue=0;
if(!double.TryParse(s,out dvalue)){
//not valid
}
Related
This question already has answers here:
Input string was not in a correct format
(9 answers)
Input string was not in a correct format in double.Parse
(5 answers)
double.Parse(123.342) System.FormatException: 'Input string was not in a correct format.' [duplicate]
(2 answers)
Closed 8 months ago.
double buy, sell, qt, tsell, tbuy, trev;
private void pro_cal()
{
sell = Convert.ToDouble(edt_sell.Text);
buy = Convert.ToDouble(edt_buy.Text);
qt = Convert.ToDouble(edt_qt.Value);
tsell = sell * qt;
tbuy = buy * qt;
trev = tsell - tbuy;
lblTbuy.Text = tbuy.ToString();
lblTsell.Text = tsell.ToString();
lblTRev.Text = trev.ToString();
}
I tried double.tryparse but that doesn't work.
I'm not able to comment yet so I'll do it here: please, show us the string you're typing
Maybe the string has a space to you could use Trim() to remove them
However, what I think is the most probably thing, is that the method is trying to parse it with another internationalization. For example, we use our number like 3,100.82 where the comma is to distinguish between thousands, millions or billions and the dot is to distinguish decimals, however, in other countries they write the numbers like 3.100,82... it's exactly the opposite.
To solve that, you have to add the culture info to the method, like this:
buy = Convert.ToDouble(edt_buy.Text, NumberStyles.AllowDecimalPoint, CultureInfo.CurrentCulture);
That may work, but please add to your post how you wrote the number since I may be wrong
This question already has answers here:
Is there a string math evaluator in .NET?
(18 answers)
operators as strings
(13 answers)
Closed 3 years ago.
I have
i = double.Parse(TextBox.Text);
but when I enter the + symbol, this error appear "System.FormatException: 'Input string was not in a correct format.'"
If I understand correctly, you have a much larger issue than you think.
Your textbox has a string. In this case, "1+1" is the value in your textbox. However, that cannot be parsed to an integer value because it contains the plus sign. The plus sign is a character, it is not an integer (0,1,2,3,4..). So, what you get is a data type conversion conflict.
From what I gather, you'd like to evaluate that expression and then store the value into into the i variable. In this case, you would like i to equal 2.
You will need to evaluate the string and convert it into a formula then use the result to store in the variable.
Here's a link to an example of a conversion formula.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/7f62b87d-a35c-4074-a0f0-84a9dd7ff0a5/convert-string-to-formula?forum=csharpgeneral
This question already has answers here:
How can I check if a string contains a character in C#?
(8 answers)
Closed 5 years ago.
I want to check if an inputted string contains a question mark.
Probably quite simple but I'm new to coding.
Use String.Contains() :
string myString = "Hello world?";
bool containsQuestionMark = myString.Contains("?"); // true
For future references, use MSDN, it's filled with good documentation.
Alternatively (to Rick's answer), if you are checking just for char occurance in a string you can use IndexOf(char):
bool containsQuestionMark = myString.IndexOf('?') != -1;
There are also some minor (negligible) performance differences between two approaches, depending on the framework version being used.
This question already has answers here:
How can I convert String to Int?
(31 answers)
Closed 6 years ago.
I am currently trying to use the value of a textbox as the value of an integer in a Timer so its like this
User inputs a value
my application reads it and uses the input as a value of interval in a timer
but its giving me this error, whats going on?
Here is what it looks like
You Need to Convert into Integer from string.Then you can use it.
exp:
int timeInterval = Convert.ToInt32(txtTime.Text);
Use can use the Variable value(timeInterval).
Thread.Sleep() takes an integer value, but you are passing the string value into the function. Any value taken from the user input like as Textbox.Text is treated as string and you need to convert that to an int.
You will have to convert your String to Int like so:
int timeInterval = Int32.Parse(txtTime.Text);
This question already has answers here:
How do I display a decimal value to 2 decimal places?
(19 answers)
Closed 7 years ago.
I need 45.7 to be 45.70
Math.Round(d, 2) have no effect.
Have tried with decimal and double type.
I would appreciate any kind of help.
You need to apply a string format:
string.Format("{0:N2}", 45.7)
Check here for docs on the string formatters