Answered by:
System.FormatException: 'The input string was not in a correct format.'

Question
-
User662762443 posted
I am trying to use TryPar, but it did not solve my problem. Follow the code below:
private void populatesLineValueIRF (StringBuilder str, List <ProcessInterest> ListProcInter) { // listProcInter.RemoveAll(o => o.ValueIR == 0 || o.ValueIR == null); List <ProcessInterest> aux = new List <ProcessInterest> (); decimal summation = 0; List <int> months = new List <int> (); if (ListProcInter.Count! = 0) { str.Append ("\ r \ nRTIRF |"); for (int i = 0; i <listProcInter.Count; i ++) { sDate = listProcInter [i].DataPayment.ToString (); datevalue = Convert.ToDateTime (sDate); months.Add (datevalue.Month); } if (! months.Contains (9)) str.Append ("|"); else { sum = 0; aux = listProcInter.FindAll (o => (Convert.ToDateTime (the.DataPath.ToString ())).Month == 9); if (Count> 1) { foreach (var j in aux) { sum = sum + decimal.Parse (j.ValorIR.ToString ()); } } else summation = decimal.Parse (aux [0] .ValorIR.ToString ()); //The error bursts in this line if (summation == 0) str.Append ("|"); else { if (sum.ToString (). Contains (',')) str.Append (sum.ToString (). Replace (",", "") + "|"); else str.Append (sum.ToString (). Replace (",", "") + "00" + "|"); } }
I do not know what can be causing this.
Tuesday, December 11, 2018 12:05 PM
Answers
-
User753101303 posted
I mean that as parsing aux[0].ValorIR.ToString() to a decimal fails, it make sense to just look at which string value is returned by this expression to better understand why it fails . A common error is to misuse ToString() returning then for example a type name.
Also parsing a string to a decimal (or to a date etc...) always depends on which country convention (ie "culture") is used. For example "1,234" can be 1.234 for some countries or 1234 for others because in some countries, the "," character is the decimal point while for others it is the thousand separator. It could likely fails for some countries when "," is none of that.
Technically speaking your code is correct. The problem is likely which value you are trying to parse and/or with which country convention.
So what if you look at the value shown by aux[0].ValorIR.ToString() ?
Edit: TryParse may or may not help depending on the value you are trying to parse and why it fails. It is used most often for user input.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 11, 2018 1:25 PM -
User-893317190 posted
Hi rtaVix,
You could use TryParse as follows.
decimal.TryParse(list[0].ValorIR.ToString(), out summation) will return false if it fails in parsing and it will return true and set the second parameter's value to the parsed decimal.
Please don't forget to add out keyword to your second parameter.
protected void Page_Load(object sender, EventArgs e) { List<MyModel> list = new List<MyModel>(); MyModel model = new MyModel() { ValorIR = 5.7 }; list.Add(model); Response.Write( myMethod(list)); } private decimal myMethod(List<MyModel> list) { decimal summation; if (decimal.TryParse(list[0].ValorIR.ToString(), out summation)) { return summation; } else { // fail in parsing
// please write your own logic } } public class MyModel { public double ValorIR { get; set; } }To see the value of list[0].ValorIR.ToString(), you could set a breakpoint and in debug mode move your mouse to the variable list.(in your case is aux) and see what the value of its first element's property ValorIR.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 12, 2018 5:45 AM
All replies
-
User753101303 posted
Hi,
When it happens you need to :
- have a look at the string value you are trying to parse - ie here what is the value for aux[0].ValorIR.ToString() ?
- check which System.Threading.Thread.CurrentThread.CurrentCulture is used. For example "12.3" could be invalid for a culture which doesn't use . as the "decimal point" symbolTuesday, December 11, 2018 12:29 PM -
User662762443 posted
I do not understand, my friend, do you have an example to explain to me better?
Tuesday, December 11, 2018 1:03 PM -
User753101303 posted
I mean that as parsing aux[0].ValorIR.ToString() to a decimal fails, it make sense to just look at which string value is returned by this expression to better understand why it fails . A common error is to misuse ToString() returning then for example a type name.
Also parsing a string to a decimal (or to a date etc...) always depends on which country convention (ie "culture") is used. For example "1,234" can be 1.234 for some countries or 1234 for others because in some countries, the "," character is the decimal point while for others it is the thousand separator. It could likely fails for some countries when "," is none of that.
Technically speaking your code is correct. The problem is likely which value you are trying to parse and/or with which country convention.
So what if you look at the value shown by aux[0].ValorIR.ToString() ?
Edit: TryParse may or may not help depending on the value you are trying to parse and why it fails. It is used most often for user input.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, December 11, 2018 1:25 PM -
User-893317190 posted
Hi rtaVix,
You could use TryParse as follows.
decimal.TryParse(list[0].ValorIR.ToString(), out summation) will return false if it fails in parsing and it will return true and set the second parameter's value to the parsed decimal.
Please don't forget to add out keyword to your second parameter.
protected void Page_Load(object sender, EventArgs e) { List<MyModel> list = new List<MyModel>(); MyModel model = new MyModel() { ValorIR = 5.7 }; list.Add(model); Response.Write( myMethod(list)); } private decimal myMethod(List<MyModel> list) { decimal summation; if (decimal.TryParse(list[0].ValorIR.ToString(), out summation)) { return summation; } else { // fail in parsing
// please write your own logic } } public class MyModel { public double ValorIR { get; set; } }To see the value of list[0].ValorIR.ToString(), you could set a breakpoint and in debug mode move your mouse to the variable list.(in your case is aux) and see what the value of its first element's property ValorIR.
Best regards,
Ackerly Xu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, December 12, 2018 5:45 AM -
User662762443 posted
Ackerly Xu, thank you for contributing my topic, your suggestion and guidance was fundamental to solve the problem that was happening to me, I am quite grateful. Thank you!
Tuesday, December 18, 2018 11:51 AM -
User662762443 posted
PatriceSc, thanks also for contributing the subject, also helped me in another problem that appeared here, thank you!
Tuesday, December 18, 2018 11:53 AM