string IFormattable.ToString(string format, IFormatProvider formatProvider)
        {
            string elementFormat = format;
            var    spec          = FormatParser.FirstSpecifierOrDefault(ref format, 'W', -1);

            if (char.ToUpperInvariant(spec.Item1) == 'W')
            {
                return(ToString(
                           spec.Item2 == -1 ? DefaultElementWidth : spec.Item2,
                           format,
                           formatProvider));
            }
            else
            {
                return(ToString(DefaultElementWidth, elementFormat, formatProvider));
            }
        }
        public string ToString(string format, IFormatProvider formatProvider)
        {
            string numberFormat = format;
            var    spec         = FormatParser.FirstSpecifierOrDefault(ref format, 'S', -1);

            if (char.ToUpperInvariant(spec.Item1) == 'S')             // or format == null
            {
                StringBuilder result = new StringBuilder();

                bool outputReal      = Re != 0;
                bool outputImaginary = Im != 0;

                if (outputReal)
                {
                    result.Append(
                        format != null
                                                ? Re.ToString(format)
                                                : Re.ToString());
                }

                if (outputImaginary)
                {
                    if (Im < 0)
                    {
                        result.Append("-");
                    }
                    else if (outputReal)
                    {
                        result.Append("+");
                    }

                    var imaginaryModulus = Math.Abs(Im);
                    if (imaginaryModulus != 1)
                    {
                        result.Append(
                            format != null
                                                        ? imaginaryModulus.ToString(format)
                                                        : imaginaryModulus.ToString());
                    }

                    result.Append("i");
                }

                if (!outputReal && !outputImaginary)
                {
                    result.Append("0");
                }

                return(result.ToString());
            }
            else
            {
                string real      = Re.ToString(numberFormat, formatProvider);
                string imaginary = Math.Abs(Im).ToString(numberFormat, formatProvider);

                return(string.Format("{0}{1}{2}i",
                                     real,
                                     Im >= 0 ? "+" : "-",
                                     imaginary));
            }
        }