Beispiel #1
0
        private void fixedFormat(DecDouble pdd, int p)
        {
            int  kend;
            bool freeFormat = ((type == 'g') || (type == 'G'));

            int i = 0;

            if (pdd.exp >= 0)
            {
                while ((output.kn <= (output.k0 + pdd.exp)) && (i < pdd.numd))
                {
                    output.append(pdd.digits[i++]);
                }

                if (output.kn <= (output.k0 + pdd.exp))
                {
                    output.append('0', (output.k0 + 1 + pdd.exp) - output.kn);
                }
            }
            else
            {
                output.append('0');
            }

            if (((p > 0) && (!freeFormat || (i < pdd.numd))) || alternate)
            {
                output.append('.');
            }

            if (p > 0)
            {
                kend = output.kn + p;

                // pad if needed
                if (pdd.exp < -1)
                {
                    output.append('0', Math.Min(p, -pdd.exp - 1));
                }

                while ((output.kn < kend) && (i < pdd.numd))
                {
                    output.append(pdd.digits[i++]);
                }

                if ((output.kn < kend) && (!freeFormat || alternate))
                {
                    output.append('0', kend - output.kn);
                }
            }

            if ((i < pdd.numd) && (pdd.digits[i] >= '5'))
            {
                roundUpFixedOutput(output);
            }
        }
Beispiel #2
0
        // private void convert(long x, int n, int m, String d)
        // {
        // if (x == 0)
        // {
        // output.append('0');
        //
        // return;
        // }
        //
        // while (x != 0)
        // {
        // output.prepend(d.charAt((int) (x & m)));
        // x = x >>> n;
        // }
        // }

        private void expFormat(DecDouble pdd, int p)
        {
            int i;

            i = 0;
            output.append(pdd.digits[i++]);

            if ((p > 0) || alternate)
            {
                output.append('.');
            }

            if (p > 0)
            {
                int kend = p + output.k0 + 2;

                while ((i < pdd.numd) && (output.kn < kend))
                {
                    output.append(pdd.digits[i++]);
                }

                if ((i == pdd.numd) && (output.kn < kend) && (((type != 'g') && (type != 'G')) || alternate))
                {
                    output.append('0', kend - output.kn);
                }
            }

            if ((i < pdd.numd) && (pdd.digits[i] >= '5'))
            {
                roundUpFixedOutput(output);

                if (output.buf[output.k0 + 1] == '0')
                {
                    output.buf[output.k0 + 1] = '.';
                    output.buf[output.k0 + 2] = '0';
                    pdd.exp++;
                    output.kn--;
                }
            }

            output.append(((type == 'G') || (type == 'E')) ? 'E' : 'e');
            output.append((pdd.exp >= 0) ? '+' : '-');

            string expStr = Convert.ToString(Math.Abs(pdd.exp), CultureInfo.InvariantCulture);

            if (expStr.Length < 2)
            {
                output.append('0');
            }

            output.append(expStr);
        }
Beispiel #3
0
        private void freeFormat(DecDouble pdd, int pprec)
        {
            int p = Math.Max(1, pprec);

            if ((pdd.exp >= -4) && (pdd.exp < p))
            {
                fixedFormat(pdd, p - pdd.exp - 1);
            }
            else
            {
                expFormat(pdd, p - 1);
            }
        }
Beispiel #4
0
        ///
        ///	 <summary> * Formats a double into a string.
        ///	 *  </summary>
        ///	 * <param name="x"> Double value to convert </param>
        ///	 * <returns> Resulting string </returns>
        ///
        public virtual string tostr(double x)
        {
            if ("diuoxX".IndexOf(type) > -1)
            {
                return(tostr((long)x));
            }

            if (dd == null)
            {
                dd = new DecDouble();
            }

            if ((type != 'a') && (type != 'A'))
            {
                dd.set(x);
            }
            else
            {
                dd.setSignAndAlt(x);
            }

            char p = '\0';

            output.init(width + 1);

            if (dd.alt != null)
            {
                output.append(dd.alt);
            }
            else if (type == 'f')
            {
                fixedFormat(dd, (prec < 0) ? 6 : prec);
            }
            else if ((type == 'e') || (type == 'E'))
            {
                expFormat(dd, (prec < 0) ? 6 : prec);
            }
            else if ((type == 'a') || (type == 'A'))
            {
                expHexFormat(x, prec);
            }
            else if ((type == 'g') || (type == 'G'))
            {
                freeFormat(dd, (prec < 0) ? 6 : prec);
            }
            else
            {
                Console.Write("f = " + type);
                throw new ArgumentException();
            }

            if (dd.sign == -1)
            {
                p = '-';
            }
            else if (addSign)
            {
                p = '+';
            }
            else if (addBlank)
            {
                p = ' ';
            }

            if (zeropad)
            {
                int nz = width - (output.kn - output.k0);

                if (p != '\0')
                {
                    nz--;
                }

                output.prepend('0', nz);
            }

            if (p != '\0')
            {
                output.prepend(p);
            }

            return(pad());
        }