Double to string conversion without scientific notation?

This is what I've got so far, seems to work, but maybe someone has a better solution.

E(?+\-\d+)$", RegexOptions. IgnoreCase|RegexOptions. ExplicitCapture|RegexOptions.

CultureInvariant); public static string ToFloatingPointString(double value) { return ToFloatingPointString(value, NumberFormatInfo. CurrentInfo); } public static string ToFloatingPointString(double value, NumberFormatInfo formatInfo) { string result = value. ToString("r", NumberFormatInfo.

InvariantInfo); Match match = rxScientific. Match(result); if (match. Success) { Debug.

WriteLine("Found scientific format: {0} => {1} {2} {3} {4}", result, match. Groups"sign", match. Groups"head", match.

Groups"tail", match. Groups"exponent"); int exponent = int. Parse(match.

Groups"exponent". Value, NumberStyles. Integer, NumberFormatInfo.

InvariantInfo); StringBuilder builder = new StringBuilder(result. Length+Math. Abs(exponent)); builder.

Append(match. Groups"sign". Value); if (exponent >= 0) { builder.

Append(match. Groups"head". Value); string tail = match.

Groups"tail". Value; if (exponent Append(tail, 0, exponent); builder. Append(formatInfo.

NumberDecimalSeparator); builder. Append(tail, exponent, tail. Length-exponent); } else { builder.

Append(tail); builder. Append('0', exponent-tail. Length); } } else { builder.

Append('0'); builder. Append(formatInfo. NumberDecimalSeparator); builder.

Append('0', (-exponent)-1); builder. Append(match. Groups"head".

Value); builder. Append(match. Groups"tail".

Value); } result = builder.ToString(); } return result; } // test code double x = 1.0; for (int I = 0; I WriteLine(x); Console. WriteLine(ToFloatingPointString(x)).

1 since does not provide solution for the following stuation (and it cannot): double d1 = 1e-200; d = d + 1; ToFloatingPointString(d) just returns 1 here. Not 1,000...........000001. – JCasso Oct 10 '09 at 0:14 3 Adding one to a very small double is just your idea, and has nothing to do with the question at hand.

If you just run it without the d=d+1, you'll see that it does in fact display 0.000.....0001. – Lucero Oct 10 '09 at 0:24 Find a way to calculate 1e-200 on runtime instead of setting a "constant" value, I will vote it up. – JCasso Oct 10 '09 at 0:33 1 No problem.

Double x = 1.0; for (int I = 0; I Therefore, computing with numbers in a similar range (like adding 1e-200 and 1e-200, or 1+1, or 1e200+1e200) does work, but mixing such values will result in rounding the smaller value away. – Lucero Oct 10 '09 at 0:54.

This is a string parsing solution where the source number (double) is converted into a string and parsed into its constituent components. It is then reassembled by rules into the full-length numeric representation. It also accounts for locale as requested.

Update: The tests of the conversions only include single-digit whole numbers, which is the norm, but the algorithm also works for something like: 239483.340901e-20 using System; using System. Text; using System. Globalization; using System.

Threading; public class MyClass { public static void Main() { Console. WriteLine(ToLongString(1.23e-2)); Console. WriteLine(ToLongString(1.23e-5)); // 0.00010234 Console.

WriteLine(ToLongString(1.23E-10)); // 0.00000001002345 Console. WriteLine(ToLongString(1.23E-20)); // 0.00000000000000000100023456 Console. WriteLine(ToLongString(5E-20)); Console.

WriteLine(""); Console. WriteLine(ToLongString(1.23E+2)); // 123 Console. WriteLine(ToLongString(1.23e5)); // 1023400 Console.

WriteLine(ToLongString(1.23E10)); // 1002345000000 Console. WriteLine(ToLongString(1.23e20)); Console. WriteLine(ToLongString(5e+20)); Console.

WriteLine(""); Console. WriteLine(ToLongString(9.1093822E-31)); // mass of an electron Console. WriteLine(ToLongString(1.237e24)); // mass of the earth Console.ReadLine(); } private static string ToLongString(double input) { string str = input.ToString().ToUpper(); // if string representation was collapsed from scientific notation, just return it: if (!str.

Contains("E")) return str; string sep = Thread.CurrentThread.CurrentCulture.NumberFormat. NumberDecimalSeparator; char decSeparator = sep.ToCharArray()0; string exponentParts = str. Split('E'); string decimalParts = exponentParts0.

Split(decSeparator); // fix missing decimal point: if (decimalParts. Length==1) decimalParts = new string{exponentParts0,"0"}; int exponentValue = int. Parse(exponentParts1); string newNumber = decimalParts0 + decimalParts1; string result; if (exponentValue > 0) { result = newNumber + GetZeros(exponentValue - decimalParts1.

Length); } else // negative exponent { result = "0" + decSeparator + GetZeros(exponentValue + decimalParts0. Length) + newNumber; result = result. TrimEnd('0'); } return result; } private static string GetZeros(int zeroCount) { if (zeroCount Abs(zeroCount); StringBuilder sb = new StringBuilder(); for (int I = 0; I.

That seems pretty similar to the one I've posted on Oct 9, at least conceptually it seems to be the same. – Lucero Oct 15 '09 at 11:13 Huh. Honestly, I noticed that it got voted down so I didn't examine the code very closely.

I did read it just now and you're right. They are close, I just chose to not use RegEx in my process and did my own string parsing. Have you tested this solution?

It's a complete console app. – Paul Sasik Oct 15 '09 at 11:29 Not yet, will do it soon... ;) – Lucero Oct 15 '09 at 15:11 1 This one is more easily read, as you don't have to grok the regex. – Gregory Oct 15 '09 at 21:52 +1 LOL @ "grok the regex" I love it.

I will make it part of my development vernacular! Thanks. – Paul Sasik Oct 15 '09 at 22:23.

In the old days when we had to write our own formatters, we'd isolate the mantissa and exponent and format them separately. In this article by Jon Skeet (yoda.arachsys.com/csharp/floatingpoint.html) he provides a link to his DoubleConverter. Cs routine that should do exactly what you want.

Skeet also refers to this at stackoverflow.com/questions/389993/extra....

– Brian Oct 15 '09 at 17:45 Yeah, but you see, the whole point of Jon's code is to display the number EXACTLY and this is kind of too much for my case. Rounding as done by the runtime when doing ToString() is just fine for me, and that's probably also why most solutions proposed here use ToString() as base for further processing. – Lucero Oct 15 '09 at 18:35 So take Jon's code and tailor it.

– ebpower Oct 16 '09 at 0:00.

The obligatory Logarithm-based solution. Note that this solution, because it involves doing math, may reduce the accuracy of your number a little bit. Not heavily tested.

Private static string DoubleToLongString(double x) { int shift = (int)Math. Log10(x); if (Math. Abs(shift) Pow(10, -shift); return "0.

". PadRight(-shift + 2, '0') + y.ToString(). Substring(2); } else { double y = x * Math.

Pow(10, 2 - shift); return y + "". PadRight(shift - 2, '0'); } } Edit: If the decimal point crosses non-zero part of the number, this algorithm will fail miserably. I tried for simple and went too far.

Thanks for the input, I'll try to implement a fully working solution like this and compare it to mine. – Lucero Oct 15 '09 at 11:15.

Try this one: public static string DoubleToFullString(double value, NumberFormatInfo formatInfo) { string valueExpSplit; string result, decimalSeparator; int indexOfDecimalSeparator, exp; valueExpSplit = value. ToString("r", formatInfo) .ToUpper() . Split(new char { 'E' }); if (valueExpSplit.

Length > 1) { result = valueExpSplit0; exp = int. Parse(valueExpSplit1); decimalSeparator = formatInfo. NumberDecimalSeparator; if ((indexOfDecimalSeparator = valueExpSplit0.

IndexOf(decimalSeparator)) > -1) { exp -= (result. Length - indexOfDecimalSeparator - 1); result = result. Replace(decimalSeparator, ""); } if (exp >= 0) result += new string('0', Math.

Abs(exp)); else { exp = Math. Abs(exp); if (exp >= result. Length) { result = "0." + new string('0', exp - result.

Length) + result; } else { result = result. Insert(result. Length - exp, decimalSeparator); } } } else result = valueExpSplit0; return result; }.

Seeing your answer I must have misunderstood your question, sorry. – URL1 Oct 9 '09 at 21:41 No, first I don't want the thousand separator and second there seems to be always a fixed number of digits after the comma. See also MSDN help for N format: msdn.microsoft.com/en-us/library/dwhawy9...#NFormatString – Lucero Oct 9 '09 at 21:44.

Just to build on what jcasso said what you can do is to adjust your double value by changing the exponent so that your favorite format would do it for you, apply the format, and than pad the result with zeros to compensate for the adjustment.

The exponent in the IEEE floating point numbers is 2-base, but the decimal numbers are 10-base. Therefore, this just doesn't work. This is also the reason why you cannot store 0.1 as exact value in a double.

Or please just provide some sample (code) if you think that I misunderstood your answer. – Lucero Oct 9 '09 at 23:16.

Being millions of programmers world wide, it's always a good practice to try search if someone has bumped into your problem already. Sometimes there's solutions are garbage, which means it's time to write your own, and sometimes there are great, such as the following: yoda.arachsys.com/csharp/DoubleConverter.cs (details: yoda.arachsys.com/csharp/floatingpoint.html).

1 This is the same as already posted by ebpower, see the comments there... ;) – Lucero Oct 19 '09 at 6:59.

I had a similar problem and this worked for me: doubleValue. ToString("F99"). TrimEnd("0".ToCharArray()) F99 may be overkill, but you get the idea.

99 is not enough, and it has to work for both before and behind the comma. – Lucero Aug 11 '11 at 21:13.

I think you need only to use IFormat with ToString(doubleVar, System.Globalization.NumberStyles. Number) example: double d = double. MaxValue; string s = d.

ToString(d, System.Globalization.NumberStyles. Number).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions