OK. I found out that it's a problem with the accuracy of the various math functions, so I was screwing with a bunch of complicated ways to get around the problem when I realized we could circumvent those issues by using string input as opposed to double input. The output is still double - only the input has been changed to string. So you would call the function using your value.ToString().
Note that this will only produce up to nine digits of accuracy. I figured that was more than enough since you were talking about truncating to three digits. If you need more than nine digits, let me know and I'll go back to the drawing board (it's possible).
Here is the modified method:
public void testTruncate()
{
for (int i = 3; i < 1000; i++)
{
string testNumber = "";
if (Math.Log10(Math.Pow(10, i)) > 9)
{
testNumber = (Math.Pow(10, i) / Math.Pow(10, i - 9) - 1).ToString();
for (int d = 0; d < Math.Log10(Math.Pow(10, i)) - testNumber.Length; d++)
{
testNumber += "0";
}
if (double.Parse(testNumber) > double.MaxValue)
{
break;
}
}
else
{
testNumber = (Math.Pow(10, i) - 1).ToString();
}
Console.WriteLine(testNumber+" truncated to "+ doTruncate(testNumber, 3, true).ToString("N0")+"\n");
}
}
public double doTruncate(string bigNumString, double numberOfDigitsToTruncateTo, bool addZeroesBack)
{
if (bigNumString.Length <= numberOfDigitsToTruncateTo)
{
return double.Parse(bigNumString);
}
string ret = bigNumString.Substring(0,(int)numberOfDigitsToTruncateTo);
if (addZeroesBack)
{
for (int i = 0; i < bigNumString.Length - numberOfDigitsToTruncateTo; i++)
{
ret += "0";
}
}
double answer = double.Parse(ret);
if (answer > double.MaxValue)
{
return -1;//value too large
}
return answer;
}
And below is the result of testTruncate from 999 through the limit of double.MaxValue
999 truncated to 999
9999 truncated to 9,990
99999 truncated to 99,900
999999 truncated to 999,000
9999999 truncated to 9,990,000
99999999 truncated to 99,900,000
999999999 truncated to 999,000,000
9999999990 truncated to 9,990,000,000
9999999990 truncated to 9,990,000,000
99999999900 truncated to 99,900,000,000
99999999900 truncated to 99,900,000,000
999999999000 truncated to 999,000,000,000
999999999000 truncated to 999,000,000,000
9999999990000 truncated to 9,990,000,000,000
9999999990000 truncated to 9,990,000,000,000
99999999900000 truncated to 99,900,000,000,000
99999999900000 truncated to 99,900,000,000,000
999999999000000 truncated to 999,000,000,000,000
999999999000000 truncated to 999,000,000,000,000
9999999990000000 truncated to 9,990,000,000,000,000
9999999990000000 truncated to 9,990,000,000,000,000
99999999900000000 truncated to 99,900,000,000,000,000
99999999900000000 truncated to 99,900,000,000,000,000
999999999000000000 truncated to 999,000,000,000,000,000
999999999000000000 truncated to 999,000,000,000,000,000
9999999990000000000 truncated to 9,990,000,000,000,000,000
9999999990000000000 truncated to 9,990,000,000,000,000,000
99999999900000000000 truncated to 99,900,000,000,000,000,000
99999999900000000000 truncated to 99,900,000,000,000,000,000
999999999000000000000 truncated to 999,000,000,000,000,000,000
999999999000000000000 truncated to 999,000,000,000,000,000,000
9999999990000000000000 truncated to 9,990,000,000,000,000,000,000
9999999990000000000000 truncated to 9,990,000,000,000,000,000,000
99999999900000000000000 truncated to 99,900,000,000,000,000,000,000
99999999900000000000000 truncated to 99,900,000,000,000,000,000,000
... and so on through ...
999999999000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 truncated to 999,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000