Ejemplo n.º 1
0
        /// <summary>
        /// Rounds a number to the specified number of decimals,
        /// formats the number in decimal format using a period and commas,
        /// and returns the result as <see cref="T:System.String" />.
        /// </summary>
        /// <param name="args"><para>
        /// The args contains 1 - 3 items: number, [decimals], [no_commas].
        /// </para>
        /// <para>
        /// Number is the number you want to round and convert to text.
        /// </para>
        /// <para>
        /// [Decimals] is the number of digits to the right of the decimal point.
        /// </para>
        /// <para>
        /// [No_commas] is a logical value that, if <see langword="true" />,
        /// prevents FIXED from including commas in the returned text.
        /// </para></param>
        /// <returns>
        /// A <see cref="T:System.String" /> value that indicates the evaluate result.
        /// </returns>
        public override object Evaluate(object[] args)
        {
            double num;

            base.CheckArgumentsLength(args);
            if (!CalcConvert.TryToDouble(args[0], out num, true))
            {
                return(CalcErrors.Value);
            }
            int  num2 = CalcHelper.ArgumentExists(args, 1) ? CalcConvert.ToInt(args[1]) : 2;
            bool flag = CalcHelper.ArgumentExists(args, 2) ? CalcConvert.ToBool(args[2]) : false;
            CalcRoundFunction function = new CalcRoundFunction();

            object[] objArray = new object[2];
            int      num3     = 0;

            if (num2 < 0)
            {
                num3        = (int)Math.Pow(10.0, (double)Math.Abs(num2));
                num        /= (double)num3;
                objArray[0] = (double)num;
                objArray[1] = 0;
            }
            else
            {
                objArray[0] = (double)num;
                objArray[1] = (int)num2;
            }
            num = (double)((double)function.Evaluate(objArray));
            if (num2 < 0)
            {
                num *= num3;
            }
            if (flag)
            {
                StringBuilder builder = new StringBuilder("F");
                if (num2 <= 0)
                {
                    builder.Append(0);
                }
                else
                {
                    builder.Append(num2);
                }
                return(((double)num).ToString(builder.ToString(), CultureInfo.CurrentCulture));
            }
            StringBuilder builder2 = new StringBuilder("N");

            if (num2 <= 0)
            {
                builder2.Append(0);
            }
            else
            {
                builder2.Append(num2);
            }
            return(((double)num).ToString(builder2.ToString(), CultureInfo.CurrentCulture));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// The function described in this Help topic converts a number to
        /// text format and applies a currency symbol.
        /// </summary>
        /// <param name="args"><para>
        /// The args contains 1 - 2 items: number, [decimals].
        /// </para>
        /// <para>
        /// Number   is a number, a reference to a cell containing a number,
        /// or a formula that evaluates to a number.
        /// </para>
        /// <para>
        /// [Decimals] is the number of digits to the right of the decimal point.
        /// If decimals is negative, number is rounded to the left of
        /// the decimal point. If you omit decimals, it is assumed to be 2.
        /// </para></param>
        /// <returns>
        /// A <see cref="T:System.String" /> value that indicates the evaluate result.
        /// </returns>
        public override object Evaluate(object[] args)
        {
            double num;

            base.CheckArgumentsLength(args);
            if (!CalcConvert.TryToDouble(args[0], out num, true))
            {
                return(CalcErrors.Value);
            }
            int num2 = CalcHelper.ArgumentExists(args, 1) ? CalcConvert.ToInt(args[1]) : 2;

            if (num2 > 0x63)
            {
                return(CalcErrors.Value);
            }
            CalcRoundFunction function = new CalcRoundFunction();

            num = (double)((double)function.Evaluate(new object[] { (double)num, (int)num2 }));
            NumberFormatInfo provider = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();

            provider.CurrencyDecimalDigits = (num2 < 0) ? 0 : num2;
            return(((double)num).ToString("C", provider));
        }