Is there a faster way to convert a string
to double
than Convert.ToDouble
?
I have monitored System.Convert.ToDouble(string)
calls and its degrading my app performance.
Convert.ToDouble("1.34515");
WORKING ANSWER FROM Jeffrey Sax :
static decimal[] decimalPowersOf10 = { 1m, 10m, 100m, 1000m, 10000m, 100000m, 1000000m };
static decimal CustomParseDecimal(string input) {
long n = 0;
int decimalPosition = input.Length;
for (int k = 0; k < input.Length; k++) {
char c = input[k];
if (c == '.')
decimalPosition = k + 1;
else
n = (n * 10) + (int)(c - '0');
}
return n / decimalPowersOf10[input.Length - decimalPosition];
}
See Question&Answers more detail:os