Exemplo n.º 1
0
        private void Test(
            string result,
            AbbreviatedNumberForm form,
            double value)
        {
            var str = AbbreviatedNumber.Format(form, value, Precision);

            str = str.Replace((char)160, ' ');
            Assert.IsTrue(result == str);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Converts a number to its abbreviated form.
        /// </summary>
        /// <param name="form">Abbreviation type.</param>
        /// <param name="value">Number value to be abbreviated.</param>
        /// <param name="precision">Number of digits that are used to express a value.</param>
        /// <returns>The formatted string.</returns>
        public static string Format(
            AbbreviatedNumberForm form,
            double value,
            int precision = DEFAULT_PRECISION)
        {
            switch (form)
            {
            case AbbreviatedNumberForm.Short:
                return(FormatShort(value, precision));

            case AbbreviatedNumberForm.Currency:
                return(FormatCurrency(value, precision));

            default:
                return(FormatLong(value, precision));
            }
        }
Exemplo n.º 3
0
        private static string GetNotAbbreviatedStr(double value, AbbreviatedNumberForm form, int precision)
        {
            if (form == AbbreviatedNumberForm.Currency)
            {
                var valuePrecision = GetValuePrecision(value);
                value = RoundToSignificantDigits(value, precision);
                string result;

                if (value == Math.Round(value))
                {
                    result = value.ToString("C0");
                }
                else
                {
                    result = value.ToString("C" + Math.Max(0, precision - valuePrecision));
                }

                return(DeleteTrailingZeros(result));
            }
            else
            {
                return(GetStr(value, precision));
            }
        }
Exemplo n.º 4
0
        private static string Format(double value, AbbreviatedNumberForm form, int precision, AbbreviationRule[] rules)
        {
            foreach (var rule in rules)
            {
                if (rule.Range <= value)
                {
                    var range  = rule.Range;
                    var result = rule.Value;
                    var p      = result.IndexOf('0');
                    result = result.Remove(p, 1);

                    while ((result != "") && (result[p] == '0'))
                    {
                        result = result.Remove(p, 1);
                        range  = range / 10;
                    }

                    string str;
                    double displayValue;

                    if (result != "")
                    {
                        str          = GetStr(value / range, precision);
                        displayValue = Convert.ToDouble(str);
                    }
                    else
                    {
                        str          = GetNotAbbreviatedStr(value, form, precision);
                        displayValue = value;
                    }

                    uint count = (uint)Math.Round(displayValue);

                    if (displayValue == count)
                    {
                        //var proc = MultiPattern.GetIndexProc();
                        var plural = MultiPattern.GetProc()(count, (int)count, 0, 0, 0, 0);

                        if (plural != rule.Plural)
                        {
                            // Wrong plural form. Find the correct one and get abbreviation again
                            var newRule = Find(rule.Range, plural, rules);

                            if (newRule != null)
                            {
                                range  = newRule.Range;
                                result = newRule.Value;
                                p      = result.IndexOf('0');
                                result = result.Remove(p, 1);

                                while ((result != "") && (result[p] == '0'))
                                {
                                    result = result.Remove(p, 1);
                                    range  = range / 10;
                                }

                                str = GetStr(value / range, precision);
                            }
                        }
                    }

                    result = result.Substring(0, p) + str + result.Substring(p);
                    p      = result.IndexOf('¤');

                    if (p >= 0)
                    {
                        result = result.Substring(0, p) + NumberFormatInfo.CurrentInfo.CurrencySymbol + result.Substring(p + 1);
                    }

                    return(result);
                }
            }

            return(GetNotAbbreviatedStr(value, form, precision));
        }