C# Input Validation Check for positive numbers - c#

I am learning C# and stuck on a problem where I have to check if the user input a VALID currency amount. i.e. no alphabetical character and no negative numbers.
So far I have everything in the program complete EXCEPT that particular input validation.
to convert the input to numeric values i have:
originalRate = Double.Parse(txtValue.Text);
then below that i am stumped, i have been messing around with:
bool isValid = Double.TryParse(txtValue.Text, );
The common compiler runtime error I get while messing around is Input string was not in a correct format. Which i know it is, that is what I am checking for.
I know this is super basic stuff (this is my first C# class). I have searched around on stack overflow and none of the similar solutions make much sense to me at this point. I have been told to use the TryParse method of the decimal class, however feel as though I am using it wrong and incomplete.
Thank you in advance for your help.

Here's how you use double.TryParse()
double d;
bool isValid = Double.TryParse(txtValue.Text, out d);
the MDSN page has some examples.
To parse currency string, you could use the second overload of double.TryParse()
and try something like
double d;
bool isValid = double.TryParse(txtValue.Text, NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"), out d);

double result;
if (double.TryParse(txtValue.text, out result))
{
// The user typed a valid number.
// Do something with it (it’s in “result”).
}
else
{
// The user typed something invalid.
MessageBox.Show("Please type in a number.");
}

Related

numeric format strings #,#0.00 vs #,0.00

I tried to figure out the basics of these numeric string formatters. So I think I understand the basics but there is one thing I'm not sure about
So, for example
#,##0.00
It turns out that it produces identical results as
#,#0.00
or
#,0.00
#,#########0.00
So my question is, why are people using the #,## so often (I see it a lot when googling)
Maybe I missed something.
You can try it out yourself here and put the following inside that main function
double value = 1234.67890;
Console.WriteLine(value.ToString("#,0.00"));
Console.WriteLine(value.ToString("#,#0.00"));
Console.WriteLine(value.ToString("#,##0.00"));
Console.WriteLine(value.ToString("#,########0.00"));
Probably because Microsoft uses the same format specifier in their documentation, including the page you linked. It's not too hard to figure out why; #,##0.00 more clearly states the programmer's intent: three-digit groups separated by commas.
What happens?
The following function is called:
public string ToString(string? format)
{
return Number.FormatDouble(m_value, format, NumberFormatInfo.CurrentInfo);
}
It is important to realize that the format is used to format the string, but your formats happen to give the same result.
Examples:
value.ToString("#,#") // 1,235
value.ToString("0,0") // 1,235
value.ToString("#") // 1235
value.ToString("0") // 1235
value.ToString("#.#")) // 1234.7
value.ToString("#.##") // 1234.68
value.ToString("#.###") // 1234.679
value.ToString("#.#####") // 1234.6789
value.ToString("#.######") // = value.ToString("#.#######") = 1234.6789
We see that
it doesn't matter whether you put #, 0, or any other digit for that matter
One occurrence means: any arbitrary large number
double value = 123467890;
Console.WriteLine(value.ToString("#")); // Prints the full number
, and . however, are treated different for double
After a dot or comma, it will only show the amount of character that are provided (or less: as for #.######).
At this point it's clear that it has to do with the programmer's intent. If you want to display the number as 1,234.68 or 1234.67890, you would format it as
"#,###.##" or "#,#.##" // 1,234.68
"####.#####" or "#.#####" // 1234.67890

validating decimal numbers in culture

can anyone please explain why, in 'en-GB' culture formatting, the decimal.Parse() function would return 15 for 1,5?
I see the same result for 'de' culture when inputting 1.5, the return is 15.
I know this is programmatically correct, I'm just trying to understand why this is correct. I've Googled this and come up with nothing :(
i'm trying to validate user inputs against culture, and populating the input field with a 0 when the parse fails, but populating the field with 15 when they've entered 1,5 doesn't feel right, i feel like it should be "failing" when a 1,5 is entered for english formating, instead of returning 15.
try {
validateSource.Text = decimal.Parse(validateThis, NumberStyles.Number, UserCulture).ToString();
} catch {
validateSource.Text = "0";
}
, is the NumberGroupSeparator in the UK culture. There's no validation in terms of how many digits are in each group. So "1,2,3" would be parsed as 123, even though that's not how we'd normally expect it to be written.
While it would make sense for parsing to check the NumberGroupSizes property, it simply doesn't.
You could emulate this check in a very primitive fashion by formatting the result of parsing, and see whether that's equal to the original input:
decimal value;
if (decimal.TryParse(text, NumberStyles.Number, culture, out value))
{
if (text != value.ToString("N", culture))
{
// Okay, looks like something's up... report an error
}
}
Of course, that would the stop someone from entering "1234.56" as it would expect "1,234.56"... you might want to check multiple pattern formats, e.g. "N" and "G", to see whether it matches any of them.

Convert.ToInt32(String) in C# not working?

I have a text box named textBox1, and In a certain case, I want to convert the string in the textbox to an integer for later use as an integer.
It's throwing an error that I can't even understand. Here is a screenshot:
(Per Request) The code is:
this.textBox1.Text = string.Concat(Int.Where(c => Char.IsNumber(c)));
this.textBox1.Text = Convert.ToInt32(this.textBox1.Text);
I would really appreciate it if you could give me an answer or fix to my code, and explain why it doesn't/does work.
Convert.ToInt32 will, by design, return an integer, not a string.
If you're just storing the result back into the text box, there's no reason at all to convert it to a number only to convert it back to a string.
This would really only be useful if you wanted to do:
int value = Convert.ToInt32(this.textBox1.Text);
That being said, you might want to use Int32.TryParse instead. This allows you to check for formatting errors instead of having exceptions raised if the user types inappropriate values.

Convert ComboBox to Decimal, C#

I am trying to create a combobox that contains text and when each text is chosen the text will equal a decimal and later be used in a math code. I am using C# inside Visual Studio.
I am a beginner and any help will be greatly appreciated.
Thanks,
Royal
That should be reasonably simple. You can use Decimal.Parse() to convert the selected string value to a decimal:
decimal val = Decimal.Parse(someComboBox.SelectedItem.ToString());
If you know that the string is definately a Decimal you can use Justin's answer, if you are unsure if it is a Decimal you could also try:
decimal ParseDecimal(string str){
decimal number;
if(Decimal.TryParse(str, out number)
{
return number;
}
return decimal.MinValue (or any other value that you know to check against)
}
Where the string you pass into the method is the combo box string.
Are you looking to convert the input string to decimal? This may help. http://msdn.microsoft.com/en-us/library/59095yyw.aspx and also Decimal.TryParse http://msdn.microsoft.com/en-us/library/9zbda557.aspx
or if you are sure you can use Convert method:
decimal val =
Convert.ToDecimal(comboBox1.SelectedItem.ToString());
But becuase you said you are unsure about what string value is (or is a decimal, or a dobule, or a simple stirng, or ...) it would be better to use Darren`s version of checking, which will check if the string is a actual decimal value. If it is, it will go into the brackets (and you can do something then), if its not, then it will go over if statement (you can add an else statement to do something else.
Mitja

int.Parse() with leading zeros

How do I prevent the code below from throwing a FormatException. I'd like to be able to parse strings with a leading zero into ints. Is there a clean way to do this?
string value = "01";
int i = int.Parse(value);
Your code runs for me, without a FormatException (once you capitalize the method properly):
string value = "01";
int i = int.Parse(value);
But this does ring an old bell; a problem I had years ago, which Microsoft accepted as a bug against localization components of Windows (not .NET). To test whether you're seeing this, run this code and let us know whether you get a FormatException:
string value = "0"; // just a zero
int i = int.Parse(value);
EDIT: here's my post from Usenet, from back in 2007. See if the symptoms match yours.
For reference, here's what we found.
The affected machine had bad data for
the registry value
[HKEY_CURRENT_USER\Control Panel
\International\sPositiveSign].
Normally, this value is an empty
REG_SZ (null-terminated string). In
this case, the string was missing its
terminator. This confused the API
function GetLocaleInfoW(), causing it
to think that '0' (ASCII number zero)
was the positive sign for the current
locale (it should normally be '+').
This caused all kinds of havoc.
You can verify this for yourself with
regedit.exe: open that reg value by
right-clicking on the value and
selecting 'Modify Binary Data'. You
should see two dots on the right
(representing the null terminator).
If you see no dots, you're affected.
Fix it by adding a terminator (four
zeros).
You can also check the value of
CultureInfo.CurrentCulture.NumberFormat.PositiveSign;
it should be '+'.
It's a bug in the Windows localization
API, not the class libs. The reg
value needs to be checked for a
terminator. They're looking at it.
...and here's a report on Microsoft Connect about the issue:
Try
int i = Int32.Parse(value, NumberStyles.Any);
int i = int.parse(value.TrimStart('0'));
TryParse will allow you to confirm the result of the parse without throwing an exception. To quote MSDN
Converts the string representation of
a number to its 32-bit signed integer
equivalent. A return value indicates
whether the operation succeeded.
To use their example
private static void TryToParse(string value)
{
int number;
bool result = Int32.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
You don't have to do anything at all. Adding leading zeroes does not cause a FormatException.
To be 100% sure I tried your code, and after correcting parse to Parse it runs just fine and doesn't throw any exception.
Obviously you are not showing actual code that you are using, so it's impossible to say where the problem is, but it's definitely not a problem for the Parse method to handle leading zeroes.
Try
int i = Convert.ToInt32(value);
Edit: Hmm. As pointed out, it's just wrapping Int32.Parse. Not sure why you're getting the FormatException, regardless.
I have the following codes that may be helpful:
int i = int.Parse(value.Trim().Length > 1 ?
value.TrimStart(new char[] {'0'}) : value.Trim());
This will trim off all extra leading 0s and avoid the case of only one 0.
For a decimal number:
Convert.ToInt32("01", 10);
// 1
For a 16 bit numbers, where leading zeros are common:
Convert.ToInt32("00000000ff", 16);
// 255

Categories