System.Xml.Xsl.Runtime.NumberFormatter.FormatSequence C# (CSharp) Method

FormatSequence() public method

Format the given xsl:number place marker
public FormatSequence ( IList val ) : string
val IList Place marker - either a sequence of ints, or a double singleton
return string
        public string FormatSequence(IList<XPathItem> val) {
            StringBuilder sb = new StringBuilder();

            // If the value was supplied directly, in the 'value' attribute, check its validity
            if (val.Count == 1 && val[0].ValueType == typeof(double)) {
                double dblVal = val[0].ValueAsDouble;
                if (!(0.5 <= dblVal && dblVal < double.PositiveInfinity)) {
                    // Errata E24: It is an error if the number is NaN, infinite or less than 0.5; an XSLT processor may signal
                    // the error; if it does not signal the error, it must recover by converting the number to a string as if
                    // by a call to the 'string' function and inserting the resulting string into the result tree.
                    return XPathConvert.DoubleToString(dblVal);
                }
            }

            if (tokens == null) {
                // Special case of the default format
                for (int idx = 0; idx < val.Count; idx++) {
                    if (idx > 0) {
                        sb.Append('.');
                    }
                    FormatItem(sb, val[idx], DefaultStartChar, 1);
                }
            } else {
                int cFormats = tokens.Count;
                TokenInfo prefix = tokens[0], suffix;

                if (cFormats % 2 == 0) {
                    suffix = null;
                } else {
                    suffix = tokens[--cFormats];
                }

                TokenInfo periodicSeparator = 2 < cFormats ? tokens[cFormats - 2] : DefaultSeparator;
                TokenInfo periodicFormat    = 0 < cFormats ? tokens[cFormats - 1] : DefaultFormat;

                if (prefix != null) {
                    prefix.AssertSeparator(true);
                    sb.Append(prefix.formatString, prefix.startIdx, prefix.length);
                }

                int valCount = val.Count;
                for (int i = 0; i < valCount; i++ ) {
                    int formatIndex = i * 2;
                    bool haveFormat = formatIndex < cFormats;

                    if (i > 0) {
                        TokenInfo thisSeparator = haveFormat ? tokens[formatIndex + 0] : periodicSeparator;
                        thisSeparator.AssertSeparator(true);
                        sb.Append(thisSeparator.formatString, thisSeparator.startIdx, thisSeparator.length);
                    }

                    TokenInfo thisFormat = haveFormat ? tokens[formatIndex + 1] : periodicFormat;
                    thisFormat.AssertSeparator(false);
                    FormatItem(sb, val[i], thisFormat.startChar, thisFormat.length);
                }

                if (suffix != null) {
                    suffix.AssertSeparator(true);
                    sb.Append(suffix.formatString, suffix.startIdx, suffix.length);
                }
            }
            return sb.ToString();
        }

Usage Example

        public string NumberFormat(IList <XPathItem> value, string formatString,
                                   double lang, string letterValue, string groupingSeparator, double groupingSize)
        {
            // REVIEW: For each execution of xsl:number new Format() object is created.
            // Then there is no AVTs we can build this object once and reuse it later to improve performance.
            NumberFormatter formatter = new NumberFormatter(formatString, (int)lang, letterValue, groupingSeparator, (int)groupingSize);

            return(formatter.FormatSequence(value));
        }
All Usage Examples Of System.Xml.Xsl.Runtime.NumberFormatter::FormatSequence