Example #1
0
            internal static string EncodeNegativeNumberStyle(NumberDataFormatter.NumberNegativeStyle numberNegativeStyle)
            {
                if (numberNegativeStyle == NumberDataFormatter.NumberNegativeStyle.Default)
                {
                    return(null);
                }

                StringBuilder sb = new StringBuilder(30);

                if ((numberNegativeStyle & NumberDataFormatter.NumberNegativeStyle.Red) == NumberDataFormatter.NumberNegativeStyle.Red)
                {
                    sb.Append("red ");
                }
                if ((numberNegativeStyle & NumberDataFormatter.NumberNegativeStyle.Brackets) == NumberDataFormatter.NumberNegativeStyle.Brackets)
                {
                    sb.Append("brackets ");
                }
                if ((numberNegativeStyle & NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku) == NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku)
                {
                    sb.Append("sankaku ");
                }
                if ((numberNegativeStyle & NumberDataFormatter.NumberNegativeStyle.CustomSymbol) == NumberDataFormatter.NumberNegativeStyle.CustomSymbol)
                {
                    sb.Append("custom ");
                }

                if (sb[sb.Length - 1] == ' ')
                {
                    sb.Length--;
                }

                return(sb.ToString());
            }
Example #2
0
 public NegativeStyleListItem(NumberDataFormatter.NumberNegativeStyle negativeStyle,
                              string sample, Color textColor, Color backColor)
     : this()
 {
     this.NegativeStyle = negativeStyle;
     this.Sample        = sample;
     this.TextColor     = textColor;
     this.BackColor     = backColor;
 }
Example #3
0
            internal static NumberDataFormatter.NumberNegativeStyle DecodeNegativeNumberStyle(string p)
            {
                NumberDataFormatter.NumberNegativeStyle flag = NumberDataFormatter.NumberNegativeStyle.Default;

                if (string.IsNullOrEmpty(p))
                {
                    return(flag);
                }

                bool hasMinusStyle = false;

                string[] tokens = p.Split(' ');

                foreach (string token in tokens)
                {
                    switch (token)
                    {
                    case "red":
                        flag |= NumberDataFormatter.NumberNegativeStyle.Red;
                        break;

                    case "brackets":
                        flag |= NumberDataFormatter.NumberNegativeStyle.Brackets;
                        break;

                    case "minus":
                        hasMinusStyle = true;
                        break;

                    case "sankaku":
                        flag |= NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku;
                        break;

                    case "custom":
                        flag |= NumberDataFormatter.NumberNegativeStyle.CustomSymbol;
                        break;
                    }
                }

                if (!hasMinusStyle)
                {
                    flag &= ~NumberDataFormatter.NumberNegativeStyle.Minus;
                }

                return(flag);
            }
Example #4
0
        /// <summary>
        /// Format specified cell
        /// </summary>
        /// <param name="cell">cell instance</param>
        /// <returns>true if cell has been formatted</returns>
        public string FormatCell(Cell cell)
        {
            bool isFormat = false;

            object data     = cell.InnerData;
            double currency = double.NaN;

            if (data is double)
            {
                isFormat = true;
                currency = (double)data;
            }
            else if (data is DateTime)
            {
                currency = (new DateTime(1900, 1, 1) - (DateTime)data).TotalDays;
                isFormat = true;
            }
            else
            {
                string str    = Convert.ToString(data).Trim();
                string number = string.Empty;

                if (str.StartsWith("$"))
                {
                    number = str.Substring(1);
                    if (double.TryParse(number, out currency))
                    {
                        isFormat       = true;
                        cell.InnerData = currency;
                    }
                }
                else
                {
                    DateTime date = new DateTime(1900, 1, 1);
                    if (DateTime.TryParse(str, out date))
                    {
                        currency = (date - new DateTime(1900, 1, 1)).TotalDays;
                        isFormat = true;
                    }
                    else
                    {
                        isFormat = double.TryParse(str, out currency);
                    }
                }
            }

            if (isFormat)
            {
                if (cell.InnerStyle.HAlign == ReoGridHorAlign.General)
                {
                    cell.RenderHorAlign = ReoGridRenderHorAlign.Right;
                }

                string prefixSymbol = null, postfixSymbol = null;
                short  decimals = 2;
                NumberDataFormatter.NumberNegativeStyle negativeStyle = NumberDataFormatter.NumberNegativeStyle.Default;
                string prefix  = null;
                string postfix = null;

                if (cell.DataFormatArgs != null && cell.DataFormatArgs is CurrencyFormatArgs)
                {
                    CurrencyFormatArgs args = (CurrencyFormatArgs)cell.DataFormatArgs;
                    prefixSymbol  = args.PrefixSymbol;
                    postfixSymbol = args.PostfixSymbol;
                    decimals      = args.DecimalPlaces;
                    negativeStyle = args.NegativeStyle;
                    prefix        = args.CustomNegativePrefix;
                    postfix       = args.CustomNegativePostfix;
                }
                //else
                //{
                //	var culture = Thread.CurrentThread.CurrentCulture;

                //	switch (culture.NumberFormat.CurrencyPositivePattern)
                //	{
                //		case 0: prefixSymbol = culture.NumberFormat.CurrencySymbol; postfixSymbol = null; break;
                //		case 1: prefixSymbol = null; postfixSymbol = culture.NumberFormat.CurrencySymbol; break;
                //		case 2: prefixSymbol = " " + culture.NumberFormat.CurrencySymbol; postfixSymbol = null; break;
                //		case 3: prefixSymbol = null; postfixSymbol = " " + culture.NumberFormat.CurrencySymbol; break;
                //	}

                //	cell.DataFormatArgs = new CurrencyFormatArgs { PrefixSymbol = prefixSymbol, PostfixSymbol = postfixSymbol, DecimalPlaces = decimals };
                //}

                if (currency < 0)
                {
                    if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Red) == NumberDataFormatter.NumberNegativeStyle.Red)
                    {
                        cell.RenderColor = SolidColor.Red;
                    }
                    else
                    {
                        cell.RenderColor = SolidColor.Transparent;
                    }
                }

                // decimal places
                string decimalPlacePart = new string('0', decimals);

                // number
                string numberPartFormat = prefixSymbol + "#,##0." + decimalPlacePart + postfixSymbol;

                if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Brackets) == NumberDataFormatter.NumberNegativeStyle.Brackets)
                {
                    numberPartFormat = (currency < 0) ? ("(" + numberPartFormat + ")") : numberPartFormat;
                }
                else if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku) == NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku)
                {
                    numberPartFormat = (currency < 0) ? ("▲ " + numberPartFormat) : numberPartFormat;
                }
                else if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.CustomSymbol) == NumberDataFormatter.NumberNegativeStyle.CustomSymbol)
                {
                    numberPartFormat = (currency < 0) ? (prefix + numberPartFormat + postfix) : numberPartFormat;
                }

                // negative
                if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Minus) == 0)
                {
                    currency = Math.Abs(currency);
                }

                return(currency.ToString(numberPartFormat));
            }
            else
            {
                return(null);
            }
        }
Example #5
0
 public NegativeStyleListItem(NumberDataFormatter.NumberNegativeStyle negativeStyle,
                              string sample, Color textColor)
     : this(negativeStyle, sample, textColor, Color.Empty)
 {
 }
Example #6
0
 public NegativeStyleListItem(NumberDataFormatter.NumberNegativeStyle negativeStyle,
     string sample, Color color)
 {
     this.negativeStyle = negativeStyle;
     this.sample = sample;
     this.color = color;
 }
Example #7
0
        /// <summary>
        /// Format specified cell
        /// </summary>
        /// <param name="cell">cell to be formatted</param>
        /// <param name="culture">culture for parsing</param>
        /// <returns>Formatted text used to display as cell content</returns>
        public string FormatCell(Cell cell, CultureInfo culture)
        {
            bool isFormat = false;

            object data     = cell.InnerData;
            double currency = double.NaN;

            if (data is double)
            {
                isFormat = true;
                currency = (double)data;
            }
            else if (data is DateTime dt)
            {
                currency = dt.ToOADate();
                isFormat = true;
            }
            else
            {
                string str = Convert.ToString(data).Trim();

                if (str.StartsWith("$"))
                {
                    string number = str.Substring(1);
                    if (double.TryParse(number, NumberStyles.Any, culture, out currency))
                    {
                        isFormat       = true;
                        cell.InnerData = currency;
                        var args = new CurrencyFormatArgs()
                        {
                            DecimalPlaces = 2
                        };
                        switch (culture.NumberFormat.CurrencyPositivePattern)
                        {
                        case 0:
                            args.PrefixSymbol = culture.NumberFormat.CurrencySymbol;
                            break;

                        case 1:
                            args.PostfixSymbol = culture.NumberFormat.CurrencySymbol;
                            break;

                        case 2:
                            args.PrefixSymbol = " " + culture.NumberFormat.CurrencySymbol;
                            break;

                        case 3:
                            args.PostfixSymbol = " " + culture.NumberFormat.CurrencySymbol;
                            break;
                        }
                        cell.DataFormatArgs = args;
                    }
                }
                else
                {
                    // Stop trying to convert datetime value to currency, #170
                    isFormat = double.TryParse(str, NumberStyles.Any, culture, out currency);
                }
            }

            if (isFormat)
            {
                if (cell.InnerStyle.HAlign == ReoGridHorAlign.General)
                {
                    cell.RenderHorAlign = ReoGridRenderHorAlign.Right;
                }

                string prefixSymbol  = null;
                string postfixSymbol = null;
                short  decimals      = 2;
                NumberDataFormatter.NumberNegativeStyle negativeStyle = NumberDataFormatter.NumberNegativeStyle.Default;
                string prefix  = null;
                string postfix = null;

                if (cell.DataFormatArgs is CurrencyFormatArgs args)
                {
                    prefixSymbol  = args.PrefixSymbol;
                    postfixSymbol = args.PostfixSymbol;
                    decimals      = args.DecimalPlaces;
                    negativeStyle = args.NegativeStyle;
                    prefix        = args.CustomNegativePrefix;
                    postfix       = args.CustomNegativePostfix;
                }

                if (currency < 0)
                {
                    if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Red) == NumberDataFormatter.NumberNegativeStyle.Red)
                    {
                        cell.RenderColor = SolidColor.Red;
                    }
                    else
                    {
                        cell.RenderColor = SolidColor.Transparent;
                    }
                }

                // decimal places
                string decimalPlacePart = new string('0', decimals);

                // number
                string numberPartFormat = prefixSymbol + "#,##0." + decimalPlacePart + postfixSymbol;

                if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Brackets) == NumberDataFormatter.NumberNegativeStyle.Brackets)
                {
                    numberPartFormat = (currency < 0) ? ("(" + numberPartFormat + ")") : numberPartFormat;
                }
                else if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku) == NumberDataFormatter.NumberNegativeStyle.Prefix_Sankaku)
                {
                    numberPartFormat = (currency < 0) ? ("▲ " + numberPartFormat) : numberPartFormat;
                }
                else if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.CustomSymbol) == NumberDataFormatter.NumberNegativeStyle.CustomSymbol)
                {
                    numberPartFormat = (currency < 0) ? (prefix + numberPartFormat + postfix) : numberPartFormat;
                }

                // negative
                if ((negativeStyle & NumberDataFormatter.NumberNegativeStyle.Minus) == 0)
                {
                    currency = Math.Abs(currency);
                }

                return(currency.ToString(numberPartFormat));
            }
            else
            {
                return(null);
            }
        }