String to decimal [closed] - c#

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
The application that I've made I've got a text box called service cost. This allows the user to enter the cost of the service they have provided and this is decimal. I'm trying to get this service cost displayed in a DGV, I've got everything else working apart from this.
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost);
The above is the code that I currently have, is there something wrong that I've done here?

From the question it is clear that txtServiceCost is the TextBox, and Convert.ToDecimal() expects a string as input so you should use txtServiceCost.Text instead for txtServiceCost. Since txtServiceCost is a control where as txtServiceCost.Text is a string
currentComputer.ServiceCost = Convert.ToDecimal(txtServiceCost.Text);
But i would like to suggest you to use decimal.TryParse
decimal userInput;
if (!decimal.TryParse(txtServiceCost.Text, out userInput))
{
// Throw some warning here that invalid input
}
currentComputer.ServiceCost = userInput;

Related

Is there a way to compare the exact value of a string in C# [closed]

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 1 year ago.
Improve this question
So I made a simple c# file reader because I was bored and added an if statement to filter the results. But when I ran it, it gave me more results than I wanted. I was supposed to get
276, 2, and there was only one line inside the file with that value, but I got multiple. I checked the file and it had lines ending with the same value. I tried string.Equals(line, "276, 2") but it gave me the same results. I doubt there isn't something in c# that doesn't solve this issue.
You can use Regex, as it's mentioned in this issue
bool result = Regex.IsMatch(line, "\\b276, 2\\b");

c# adding money together removes the 0 at the end [closed]

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

Get particular part(s) from String in C# [closed]

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 5 years ago.
Improve this question
I'm reading an email and performing a few operations.
In email it can't be in defined format as below,
Email formats:
Hi Team,
Please create 7025-45-365-14, 9851-98-524-12
5741-55-452-45
Thanks
MailItem.Body = "Hi Team,\r\n\r\n \r\n\r\nPlease create 7025-45-365-14, 9851-98-524-12\r\n\r\n5741-55-452-45.\r\n\r\n \r\n\r\nThanks\r\n"
My goal is to exact only the string parts into a loop one by one as below,
1st loop > 7025-45-365-14
2nd loop > 9851-98-524-12
3rd loop > 5741-55-452-45
I tried various logic but I can't extract as I need. Can someone help me?
To extract digits in that pattern (4-2-3-2)
foreach (var m in new Regex(#"\b(\d{4}-\d{2}-\d{3}-\d{2})\b").Matches(mail))
Console.WriteLine(m.Value);

How to validate textbox to enter only numerical or one string value that is 'AB' [closed]

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 5 years ago.
Improve this question
How to validate textbox to enter only numerical or one string value that is 'AB'
means the in that textbox you can enter either numerical or 'AB'
so.. how to do
Other than the commented suggestion for RegEx, you can use TryParse() method like
int number;
if(Int32.TryParse(txt1.Text, out number) || txt1.Text == "AB")
//Your code here

C# Converting string to int fails [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
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
Improve this question
I am working on a little project, that at a certain point need to convert a string to int.
I tried the following things:
//first try
value1 = int.TryParse(value[0].tostring(), out i)
//second
value1 = Convert.ToInt32(value[0].tostring())
//third
value1 = int.Parse(value[0].tostring())
I even wrote my own conversion method because I was at a loss.
The values I am trying to convert are queried from a MySQL database.
Thank for your help
EDIT:
I know tryparse should have 2 params.
And the error iam getting is a formatexception
Input string was not in a correct format.
I got that on all the tries.
the value in my test case is 2500
Keep in mind that that number is received from a db
I tried the above snippets while using a hard coded value. And that works fine.
EDIT 2:
//http://imgur.com/NSyg2rJ
One:
The signature of int.TryParse() is incorrect:
int result = 0;
bool parsedSuccessfully = Int32.TryParse(value[0].ToString(), out result);
//If successful, result will hold the value.
Two:
The signature of Convert.ToInt32() is correct, minus the case of the .ToString() method. This is failing because value or value[0] is null, or it cannot convert something like the string "NotAnInt" to an int.
Three:
The signature of int.Parse() is correct, but is failing for the same reasons as two.
Try this -
int value1;
if (Int.TryParse(value[0].ToString(), out value1))
{
//conversion successful
}

Categories

Resources