Maybe I am trying to jump ahead to fast and need to slow back down a touch.
In my honest opinion, numbers are easy concept-wise. You just weren't aware that what you were doing with numeric manipulation was as difficult as it was, easy first mistake.
I'd suggest you keep going in the direction you're headed. However overwhelming it may be at first, the more you see, the more you may understand if you come across certain code issues that help you learn after a bit of troubleshooting.
Its the second piece of code I am looking to understand. This is the part I do not fully understand. From what I can make out it is parsing a double to string, then converting it to an array?
Close, but not quite. It's important to see where the brackets match up.
And inside of the Parse() function, we've got:
d.ToString("0." + new string(Enumerable.Repeat('0', places).ToArray()))
And inside of the ToString() method, we've got our string formatting data. More information on String formatting here: http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
The String formatting:
"0." + new string(Enumerable.Repeat('0', places).ToArray())
2) new string(Enumerable.Repeat('0', places).ToArray())
In the end, these both evaluate to this kind of format, depending on the second part:
In that second part we have:
new string(Enumerable.Repeat('0', places).ToArray())
Here, it's important to see that we are using a String class constructor here that takes a Char array as an input argument. The only thing is... Enumerable.Repeat
returns an IEnumerable result, and not a String array, so we need to cast that to an Array using the ToArray() function.
Enumerable.Repeat takes the '0' and repeats it however many times specified by the places
variable as our second argument for that function.
So we end up with a repeated number of '0's that we cast to an array of char elements of '0', and when we give this to the constructor for the String class which takes a char array as an input argument, it takes this Char array, and combines it into a single string.
So say, places was a value of 3.
We would have "0." and the Repeated result of 3 '0's which would give us "0." + "000", and thus "0.000" which would format our decimal value to 3 decimal places if you understand the string formatting.
More information on the String constructor overload I was using here: http://msdn.microsoft.com/en-us/library/ttyxaek9.aspx