Exemplo n.º 1
0
        public static String toLocale(long self, string pattern)
        {
            // if pattern is "B" format as bytes
            if (pattern != null && pattern.Length == 1 && pattern[0] == 'B')
            {
                return(toLocaleBytes(self));
            }

            // get current locale
            Locale           locale = Locale.cur();
            NumberFormatInfo df     = locale.dec();

            // get default pattern if necessary
            if (pattern == null)
            {
                pattern = Env.cur().locale(Sys.m_sysPod, "int", "#,###");
            }

            // parse pattern and get digits
            NumPattern p = NumPattern.parse(pattern);
            NumDigits  d = new NumDigits(self);

            // route to common FanNum method
            return(FanNum.toLocale(p, d, df));
        }
Exemplo n.º 2
0
        public static string toLocale(double self, string pattern)
        {
            try
            {
                // get current locale
                Locale           locale = Locale.cur();
                NumberFormatInfo df     = locale.dec();

                // handle special values
                if (System.Double.IsNaN(self))
                {
                    return(df.NaNSymbol);
                }
                if (self == System.Double.PositiveInfinity)
                {
                    return(df.PositiveInfinitySymbol);
                }
                if (self == System.Double.NegativeInfinity)
                {
                    return(df.NegativeInfinitySymbol);
                }

                // get default pattern if necessary
                if (pattern == null)
                {
                    pattern = Env.cur().locale(Sys.m_sysPod, "float", "#,###.0##");
                }

                // TODO: if value is < 10^-3 or > 10^7 it will be
                // converted to exponent string, so just bail on that
                string str = Double.toString(self);
                if (str.IndexOf('E') > 0)
                {
                    str = self.ToString("0.#########");
                }

                // parse pattern and get digits
                NumPattern p = NumPattern.parse(pattern);
                NumDigits  d = new NumDigits(str);

                // route to common FanNum method
                return(FanNum.toLocale(p, d, df));
            }
            catch (Exception e)
            {
                Err.dumpStack(e);
                return(self.ToString());
            }
        }
Exemplo n.º 3
0
        public static string toLocale(BigDecimal self, string pattern)
        {
            // get current locale
            Locale           locale = Locale.cur();
            NumberFormatInfo df     = locale.dec();

            // get default pattern if necessary
            if (pattern == null)
            {
                pattern = Env.cur().locale(Sys.m_sysPod, "decimal", "#,###.0##");
            }

            // parse pattern and get digits
            NumPattern p = NumPattern.parse(pattern);
            NumDigits  d = new NumDigits(self);

            // route to common FanNum method
            return(FanNum.toLocale(p, d, df));
        }
Exemplo n.º 4
0
        internal static string toLocale(NumPattern p, NumDigits d, NumberFormatInfo df)
        {
            // string buffer
              StringBuilder s = new StringBuilder();
              if (d.negative) s.Append(df.NegativeSign);

              // if we have more frac digits then maxFrac, then round off
              d.round(p.maxFrac);

              // if we have an optional integer part, and only
              // fractional digits, then don't include leading zero
              int start = 0;
              if (p.optInt && d.zeroInt()) start = d.dec;

              // if min required fraction digits are zero and we
              // have nothing but zeros, then truncate to a whole number
              if (p.minFrac == 0 && d.zeroFrac(p.maxFrac)) d.size = d.dec;

              // leading zeros
              for (int i=0; i<p.minInt-d.dec; ++i) s.Append('0');

              // walk thru the digits and apply locale symbols
              bool dec = false;
              for (int i=start; i<d.size; ++i)
              {
            if (i < d.dec)
            {
              if ((d.dec - i) % p.group == 0 && i > 0)
            s.Append(df.NumberGroupSeparator);
            }
            else
            {
              if (i == d.dec && p.maxFrac > 0)
              {
            s.Append(df.NumberDecimalSeparator);
            dec = true;
              }
              if (i-d.dec >= p.maxFrac) break;
            }
            s.Append(d.digits[i]);
              }

              // trailing zeros
              for (int i=0; i<p.minFrac-d.fracSize(); ++i)
              {
            if (!dec) { s.Append(df.NumberDecimalSeparator); dec = true; }
            s.Append('0');
              }

              // handle #.# case
              if (s.Length == 0) return "0";

              return s.ToString();
        }
Exemplo n.º 5
0
        internal static string toLocale(NumPattern p, NumDigits d, NumberFormatInfo df)
        {
            // string buffer
            StringBuilder s = new StringBuilder();

            if (d.negative)
            {
                s.Append(df.NegativeSign);
            }

            // if we have more frac digits then maxFrac, then round off
            d.round(p.maxFrac);

            // if we have an optional integer part, and only
            // fractional digits, then don't include leading zero
            int start = 0;

            if (p.optInt && d.zeroInt())
            {
                start = d.dec;
            }

            // if min required fraction digits are zero and we
            // have nothing but zeros, then truncate to a whole number
            if (p.minFrac == 0 && d.zeroFrac(p.maxFrac))
            {
                d.size = d.dec;
            }

            // leading zeros
            for (int i = 0; i < p.minInt - d.dec; ++i)
            {
                s.Append('0');
            }

            // walk thru the digits and apply locale symbols
            bool dec = false;

            for (int i = start; i < d.size; ++i)
            {
                if (i < d.dec)
                {
                    if ((d.dec - i) % p.group == 0 && i > 0)
                    {
                        s.Append(df.NumberGroupSeparator);
                    }
                }
                else
                {
                    if (i == d.dec && p.maxFrac > 0)
                    {
                        s.Append(df.NumberDecimalSeparator);
                        dec = true;
                    }
                    if (i - d.dec >= p.maxFrac)
                    {
                        break;
                    }
                }
                s.Append(d.digits[i]);
            }

            // trailing zeros
            for (int i = 0; i < p.minFrac - d.fracSize(); ++i)
            {
                if (!dec)
                {
                    s.Append(df.NumberDecimalSeparator); dec = true;
                }
                s.Append('0');
            }

            // handle #.# case
            if (s.Length == 0)
            {
                return("0");
            }

            return(s.ToString());
        }
Exemplo n.º 6
0
        public static string toLocale(BigDecimal self, string pattern)
        {
            // get current locale
              Locale locale = Locale.cur();
              NumberFormatInfo df = locale.dec();

              // get default pattern if necessary
              if (pattern == null)
            pattern = Env.cur().locale(Sys.m_sysPod, "decimal", "#,###.0##");

              // parse pattern and get digits
              NumPattern p = NumPattern.parse(pattern);
              NumDigits d = new NumDigits(self);

              // route to common FanNum method
              return FanNum.toLocale(p, d, df);
        }
Exemplo n.º 7
0
        public static string toLocale(double self, string pattern)
        {
            try
              {
            // get current locale
            Locale locale = Locale.cur();
            NumberFormatInfo df = locale.dec();

            // handle special values
            if (System.Double.IsNaN(self)) return df.NaNSymbol;
            if (self == System.Double.PositiveInfinity) return df.PositiveInfinitySymbol;
            if (self == System.Double.NegativeInfinity) return df.NegativeInfinitySymbol;

            // get default pattern if necessary
            if (pattern == null)
              pattern = Env.cur().locale(Sys.m_sysPod, "float", "#,###.0##");

            // TODO: if value is < 10^-3 or > 10^7 it will be
            // converted to exponent string, so just bail on that
            string str = Double.toString(self);
            if (str.IndexOf('E') > 0)
              str = self.ToString("0.#########");

            // parse pattern and get digits
            NumPattern p = NumPattern.parse(pattern);
            NumDigits d = new NumDigits(str);

            // route to common FanNum method
            return FanNum.toLocale(p, d, df);
              }
              catch (Exception e)
              {
            Err.dumpStack(e);
            return self.ToString();
              }
        }
Exemplo n.º 8
0
        public static String toLocale(long self, string pattern)
        {
            // if pattern is "B" format as bytes
              if (pattern != null && pattern.Length == 1 && pattern[0] == 'B')
            return toLocaleBytes(self);

              // get current locale
              Locale locale = Locale.cur();
              NumberFormatInfo df = locale.dec();

              // get default pattern if necessary
              if (pattern == null)
            pattern = Env.cur().locale(Sys.m_sysPod, "int", "#,###");

              // parse pattern and get digits
              NumPattern p = NumPattern.parse(pattern);
              NumDigits d = new NumDigits(self);

              // route to common FanNum method
              return FanNum.toLocale(p, d, df);
        }