// Benchmark:
// int.Parse("400") 123.07 ns
// IntParseFast("400") 2.87 ns
public static int IntParseFast(string value)
int result = 0;
for (int i = 0; i < value.Length; i++)
char letter = value[i];
result = 10 * result + (letter - 48);
return result;
public static int IntParseFast(char value)
int result = 0;
result = 10 * result + (value - 48);
return result;
Um… That’s the point? Value is an argument, so it will always purposely evaluate to value - 48.
The Char ‘1’ = 49
The Number 1 = 1
49 - 48 = 1
The Char ‘2’ = 50
The Number 2 = 2
50 - 48 = 2
And so on.
Passing the value ‘1’ as a char will give you 1 back as an int fast.
However, won’t it work without multiplication?
public static int IntParseFast(char value)
int result = 0;
result = result + (value - 48);
return result;
enteredValueCommodityAmount=int.Parse("123456789");
Debug.Log(enteredValueCommodityAmount.GetType());
Debug.Log(enteredValueCommodityAmount);
return enteredValueCommodityAmount;
this is the error i am receiving:
FormatException: Input string was not in the correct format
I have tried:
Int32.Parse, system.Int32 and many more that I have found in examples of and in the MSDN documentation and all lead back to the same error.
FormatException: Input string was not in the correct format
int.TryParse throws this error:
error CS1501: No overload for method TryParse' takes 1’ arguments
I was calling my string from a variable, but recently i just tried using a string, like the one above. All to the same effect…
any ideas?
What am i missing?
thanks for the link, i checked here:
Wikipedia
couldn’t remember the tags for newline or code. So it is nice that you provided an answer for the format issue.
But why the trolling on the question, did I not present the question well?
I didn’t see the need to create a new thread for the same problem, maybe others might find this problem in a similar situation.
i am missing the “out parameter” in the second error, and this seems to be where the problem lies for TryParse, but for the others above Int32.parse etc i am confused why my string is not valid, i was hoping to get some positive feedback
But really, Bravo on stating the obvious and only offering advice on the formatting. Thanks so much.
thanks for the link, i checked here:
Wikipedia
couldn’t remember the tags for newline or code. So it is nice that you provided an answer for the format issue.
But why the trolling on the question, did I not present the question well?
I didn’t see the need to create a new thread for the same problem, maybe others might find this problem in a similar situation.
i am missing the “out parameter” in the second error, and this seems to be where the problem lies for TryParse, but for the others above Int32.parse etc i am confused why my string is not valid, i was hoping to get some positive feedback
But really, Bravo on stating the obvious and only offering advice on the formatting. Thanks so much.
The threads original question was about how to convert strings to integers. You actually have a new problem as your integer string is not being parsed which warrants a new thread.
However, with that being said… I don’t see any reason for your string to fail the parse. There is a possibility that your NumberStyles are messed up. Is the TryParse method working for you?