You can convert your 'double result to 'decimal to be able to produce a string result with more digits. A big 'double is going to be rendered with the mantissa/exponent notation because that is what it is, under-the-hood.
private string PowerDoubleToDecimal(double doubleValue, double toPower, string formatString)
{
decimal result = Convert.ToDecimal(Math.Pow(doubleValue, toPower));
return string.Format(result.ToString(), formatString);
}
string test1 = PowerDoubleToDecimal(95.123, 12.0, "C");
Console.WriteLine(test1);
string test1a = PowerDoubleToDecimal(0.4321, 20.0, "C");
Console.WriteLine(test1a);
548815620517732000000000
0.0000000514855464107695
Jon Skeet has public domain code, 'DoubleConverter:' [
^] to get a much better 'transcription' from a Double to String. Using Skeet's code, I get these results from your two trials:
private string DblToExactString(double doubleValue, double toPower)
{
double result = Math.Pow(doubleValue, toPower);
return SkeetDoubleConverter.ToExactString(result);
}
string test2 = DblToExactString(95.123, 12.0);
Console.WriteLine(test2);
string test2a = DblToExactString(0.4321, 20.0);
Console.WriteLine(test2a);
548815620517732158537728
0.0000000514855464107695238993420865959704801895213677198626101016998291015625
And, I note the results are not the same as yours. That leads to the question: if you need this high-precision for some
mission-critical application (as in aviation, finance, health-care, etc.), would you bet your company's future on 3rd. party code (even, as is the case with Jon Skeet, a guru's guru in .NET) if it comes from a source you believe reliable ?
I wouldn't :) I'd want to spend the money for a library from a very well-known company with years of experience in whatever specialized high-precision performance I needed.
There are many special libraries available for handling high-precision floating point numbers (some open source, some offering both free and commercial versions, some purely commercial). For links to other libraries: [
^].