private String FormatNumber(double value, string formatPattern, String formatName) { string ns = string.Empty, local = string.Empty; if (formatName != null) { string prefix; PrefixQName.ParseQualifiedName(formatName, out prefix, out local); ns = LookupNamespace(prefix); } DecimalFormat formatInfo = this.processor.RootAction.GetDecimalFormat(new XmlQualifiedName(local, ns)); if (formatInfo == null) { if (formatName != null) { throw new XsltException(Res.Xslt_NoDecimalFormat, formatName); } formatInfo = new DecimalFormat(new NumberFormatInfo(), '#', '0', ';'); } int[] groupsize = { ValidateFormat(ref formatPattern, formatInfo, value < 0) }; NumberFormatInfo numberInfo = formatInfo.info; numberInfo.NumberGroupSizes = groupsize; String result = value.ToString(formatPattern, numberInfo); if (formatInfo.zeroDigit != '0') { StringBuilder builder = new StringBuilder(result.Length); int startingLetter = Convert.ToInt32(formatInfo.zeroDigit) - Convert.ToInt32('0');; for (int i = 0; i < result.Length; i++) { if (result[i] >= '0' && result[i] <= '9') { builder.Append((char)(Convert.ToInt32(result[i]) + startingLetter)); } else { builder.Append(result[i]); } } result = builder.ToString(); } return(result); }
protected void CompileDecimalFormat(Compiler compiler) { NumberFormatInfo info = new NumberFormatInfo(); DecimalFormat format = new DecimalFormat(info, '#', '0', ';'); XmlQualifiedName Name = null; NavigatorInput input = compiler.Input; if (input.MoveToFirstAttribute()) { do { Debug.TraceAttribute(input); if (!Keywords.Equals(input.Prefix, input.Atoms.Empty)) { continue; } string name = input.LocalName; string value = input.Value; if (Keywords.Equals(name, input.Atoms.Name)) { Name = compiler.CreateXPathQName(value); } else if (Keywords.Equals(name, input.Atoms.DecimalSeparator)) { info.NumberDecimalSeparator = value; } else if (Keywords.Equals(name, input.Atoms.GroupingSeparator)) { info.NumberGroupSeparator = value; } else if (Keywords.Equals(name, input.Atoms.Infinity)) { info.PositiveInfinitySymbol = value; } else if (Keywords.Equals(name, input.Atoms.MinusSign)) { info.NegativeSign = value; } else if (Keywords.Equals(name, input.Atoms.NaN)) { info.NaNSymbol = value; } else if (Keywords.Equals(name, input.Atoms.Percent)) { info.PercentSymbol = value; } else if (Keywords.Equals(name, input.Atoms.PerMille)) { info.PerMilleSymbol = value; } else if (Keywords.Equals(name, input.Atoms.Digit)) { if (CheckAttribute(value.Length == 1, compiler)) { format.digit = value[0]; } } else if (Keywords.Equals(name, input.Atoms.ZeroDigit)) { if (CheckAttribute(value.Length == 1, compiler)) { format.zeroDigit = value[0]; } } else if (Keywords.Equals(name, input.Atoms.PatternSeparator)) { if (CheckAttribute(value.Length == 1, compiler)) { format.patternSeparator = value[0]; } } }while(input.MoveToNextAttribute()); info.NegativeInfinitySymbol = String.Concat(info.NegativeSign, info.PositiveInfinitySymbol); if (Name == null) { Name = new XmlQualifiedName(null, null); } compiler.AddDecimalFormat(Name, format); input.ToParent(); } }
internal void AddDecimalFormat(XmlQualifiedName name, DecimalFormat formatinfo) { this.rootAction.AddDecimalFormat(name, formatinfo); }
private int ValidateFormat(ref String format, DecimalFormat formatinfo, bool negative) { String pospattern = null; char currencySymbol = (char)0x00A4; int commaindex = 0; bool integer = true, posDecimalSeparator = false; bool sawpattern = false, sawzerodigit = false, sawdigit = false, sawdecimalseparator = false, sawunderscore = false, sawstar = false; char patternseparator, decimalseparator, groupseparator, percentsymbol; if (formatinfo != null) { patternseparator = formatinfo.patternSeparator; percentsymbol = formatinfo.info.PercentSymbol[0]; decimalseparator = formatinfo.info.NumberDecimalSeparator[0]; groupseparator = formatinfo.info.NumberGroupSeparator[0]; } else { percentsymbol = '%'; patternseparator = ';'; decimalseparator = '.'; groupseparator = ','; } if (format.Length == 0) { throw new XsltException(Res.Xslt_InvalidFormat); } StringBuilder temp = new StringBuilder(); int groupingSize = 0; int length = format.Length; int decimalindex = length; for (int i = 0; i < length; i++) { char ch = format[i]; if (ch == formatinfo.digit) { if (sawzerodigit && integer) { throw new XsltException(Res.Xslt_InvalidFormat1, format); } sawdigit = true; temp.Append('#'); continue; } if (ch == formatinfo.zeroDigit) { if (sawdigit && !integer) { throw new XsltException(Res.Xslt_InvalidFormat2, format); } sawzerodigit = true; temp.Append('0'); continue; } if (ch == patternseparator) { CheckInteger(sawzerodigit, sawdigit); if (sawpattern) { throw new XsltException(Res.Xslt_InvalidFormat3, format); } sawpattern = true; if (i == 0 || i == length - 1) { throw new XsltException(Res.Xslt_InvalidFormat4, format); } RemoveTrailingComma(temp, commaindex, decimalindex, ref groupingSize); pospattern = temp.ToString(); posDecimalSeparator = sawdecimalseparator; temp.Length = 0; decimalindex = length - i - 1; commaindex = 0; sawdigit = sawzerodigit = sawdecimalseparator = sawstar = sawunderscore = false; integer = true; continue; } if (ch == decimalseparator) { if (sawdecimalseparator) { throw new XsltException(Res.Xslt_InvalidFormat5, format); } decimalindex = temp.Length; sawdecimalseparator = true; sawdigit = sawzerodigit = integer = false; temp.Append('.'); continue; } if (ch == groupseparator) { commaindex = temp.Length; temp.Append(','); continue; } if (ch == formatinfo.info.PercentSymbol[0]) { temp.Append('%'); continue; } if (ch == formatinfo.info.PerMilleSymbol[0]) { temp.Append((char)0x2030); continue; } if (ch == '*') { if (sawunderscore) { throw new XsltException(Res.Xslt_InvalidFormat6, format); } sawstar = true; continue; } if (ch == '_') { if (sawstar) { throw new XsltException(Res.Xslt_InvalidFormat6, format); } sawunderscore = true; temp.Append(ch); continue; } if ((int)ch < 0x0000 && (int)ch > 0xfffd || ch == currencySymbol) { throw new XsltException(Res.Xslt_InvalidFormat7, format, ch.ToString()); } temp.Append(ch); } //CheckInteger(sawzerodigit, sawdigit); if (sawpattern) { if (negative) { if (!sawdecimalseparator) { formatinfo.info.NumberDecimalDigits = 0; } RemoveTrailingComma(temp, commaindex, decimalindex, ref groupingSize); if (temp.Length > 0 && temp[0] == '-') { temp.Remove(0, 1); } else { formatinfo.info.NegativeSign = String.Empty; } format = temp.ToString(); } else { if (!posDecimalSeparator) { formatinfo.info.NumberDecimalDigits = 0; } format = pospattern; } } else { RemoveTrailingComma(temp, commaindex, decimalindex, ref groupingSize); format = temp.ToString(); if (!sawdecimalseparator) { formatinfo.info.NumberDecimalDigits = 0; } } return(groupingSize); }
private String FormatNumber(double value, string formatPattern, String formatName) { string ns = string.Empty, local = string.Empty; if (formatName != null) { string prefix; PrefixQName.ParseQualifiedName(formatName, out prefix, out local); ns = LookupNamespace(prefix); } DecimalFormat formatInfo = this.processor.RootAction.GetDecimalFormat(new XmlQualifiedName(local, ns)); if (formatInfo == null) { if (formatName != null) { throw new XsltException(Res.Xslt_NoDecimalFormat, formatName); } formatInfo = new DecimalFormat(new NumberFormatInfo(), '#', '0', ';'); } int[] groupsize = {ValidateFormat(ref formatPattern, formatInfo, value < 0)}; NumberFormatInfo numberInfo = formatInfo.info; numberInfo.NumberGroupSizes = groupsize; String result = value.ToString(formatPattern, numberInfo); if (formatInfo.zeroDigit != '0') { StringBuilder builder = new StringBuilder(result.Length); int startingLetter = Convert.ToInt32(formatInfo.zeroDigit) - Convert.ToInt32('0');; for(int i = 0; i < result.Length; i++) { if (result[i] >= '0' && result[i] <= '9') { builder.Append((char)(Convert.ToInt32(result[i]) + startingLetter)); } else { builder.Append(result[i]); } } result = builder.ToString(); } return result; }
private int ValidateFormat(ref String format, DecimalFormat formatinfo, bool negative) { String pospattern = null; char currencySymbol = (char)0x00A4; int commaindex = 0; bool integer = true, posDecimalSeparator = false; bool sawpattern = false, sawzerodigit= false, sawdigit = false, sawdecimalseparator = false, sawunderscore = false, sawstar = false; char patternseparator, decimalseparator, groupseparator, percentsymbol; if ( formatinfo != null ) { patternseparator = formatinfo.patternSeparator; percentsymbol = formatinfo.info.PercentSymbol[0]; decimalseparator = formatinfo.info.NumberDecimalSeparator[0] ; groupseparator = formatinfo.info.NumberGroupSeparator[0]; } else { percentsymbol = '%'; patternseparator = ';'; decimalseparator = '.'; groupseparator = ','; } if ( format.Length == 0 ) { throw new XsltException(Res.Xslt_InvalidFormat); } StringBuilder temp = new StringBuilder(); int groupingSize = 0; int length = format.Length; int decimalindex = length; for (int i= 0; i < length; i++ ) { char ch = format[i]; if ( ch == formatinfo.digit ) { if (sawzerodigit && integer) { throw new XsltException(Res.Xslt_InvalidFormat1, format); } sawdigit = true; temp.Append('#'); continue; } if (ch == formatinfo.zeroDigit ) { if (sawdigit && !integer) { throw new XsltException(Res.Xslt_InvalidFormat2, format); } sawzerodigit = true; temp.Append('0'); continue; } if (ch == patternseparator ) { CheckInteger(sawzerodigit, sawdigit); if (sawpattern) { throw new XsltException(Res.Xslt_InvalidFormat3, format); } sawpattern = true; if (i == 0 || i == length - 1) { throw new XsltException(Res.Xslt_InvalidFormat4, format); } RemoveTrailingComma(temp, commaindex, decimalindex, ref groupingSize); pospattern = temp.ToString(); posDecimalSeparator = sawdecimalseparator; temp.Length = 0; decimalindex = length - i - 1; commaindex = 0; sawdigit = sawzerodigit = sawdecimalseparator = sawstar = sawunderscore = false; integer = true; continue; } if (ch == decimalseparator ) { if ( sawdecimalseparator ) { throw new XsltException(Res.Xslt_InvalidFormat5, format); } decimalindex = temp.Length; sawdecimalseparator = true; sawdigit = sawzerodigit = integer = false; temp.Append('.'); continue; } if (ch == groupseparator ) { commaindex = temp.Length; temp.Append(','); continue; } if (ch == formatinfo.info.PercentSymbol[0] ) { temp.Append('%'); continue; } if (ch == formatinfo.info.PerMilleSymbol[0] ) { temp.Append((char)0x2030); continue; } if (ch == '*' ) { if (sawunderscore) { throw new XsltException(Res.Xslt_InvalidFormat6, format); } sawstar = true; continue; } if (ch == '_' ) { if (sawstar) { throw new XsltException(Res.Xslt_InvalidFormat6, format); } sawunderscore = true; temp.Append(ch); continue; } if ((int) ch < 0x0000 && (int) ch > 0xfffd || ch == currencySymbol) { throw new XsltException(Res.Xslt_InvalidFormat7, format, ch.ToString()); } temp.Append(ch); } //CheckInteger(sawzerodigit, sawdigit); if ( sawpattern ) { if (negative) { if (!sawdecimalseparator) { formatinfo.info.NumberDecimalDigits = 0; } RemoveTrailingComma(temp, commaindex, decimalindex, ref groupingSize); if (temp.Length > 0 && temp[0] == '-') { temp.Remove(0,1); } else { formatinfo.info.NegativeSign = String.Empty; } format = temp.ToString(); } else { if (!posDecimalSeparator) { formatinfo.info.NumberDecimalDigits = 0; } format = pospattern; } } else { RemoveTrailingComma(temp, commaindex, decimalindex, ref groupingSize); format = temp.ToString(); if (!sawdecimalseparator) { formatinfo.info.NumberDecimalDigits = 0; } } return groupingSize; }
internal void AddDecimalFormat(XmlQualifiedName name, DecimalFormat formatinfo) { DecimalFormat exist = (DecimalFormat) this.decimalFormatTable[name]; if (exist != null) { NumberFormatInfo info = exist.info; NumberFormatInfo newinfo = formatinfo.info; if (info.NumberDecimalSeparator != newinfo.NumberDecimalSeparator || info.NumberGroupSeparator != newinfo.NumberGroupSeparator || info.PositiveInfinitySymbol != newinfo.PositiveInfinitySymbol || info.NegativeSign != newinfo.NegativeSign || info.NaNSymbol != newinfo.NaNSymbol || info.PercentSymbol != newinfo.PercentSymbol || info.PerMilleSymbol != newinfo.PerMilleSymbol || exist.zeroDigit != formatinfo.zeroDigit || exist.digit != formatinfo.digit || exist.patternSeparator != formatinfo.patternSeparator ) { throw new XsltException(Res.Xslt_DupDecimalFormat, name.ToString()); } } this.decimalFormatTable[name] = formatinfo; }
protected void CompileDecimalFormat(Compiler compiler){ NumberFormatInfo info = new NumberFormatInfo(); DecimalFormat format = new DecimalFormat(info, '#', '0', ';'); XmlQualifiedName Name = null; NavigatorInput input = compiler.Input; if (input.MoveToFirstAttribute()) { do { Debug.TraceAttribute(input); if (! Keywords.Equals(input.Prefix, input.Atoms.Empty)) continue; string name = input.LocalName; string value = input.Value; if (Keywords.Equals(name, input.Atoms.Name)) { Name = compiler.CreateXPathQName(value); } else if (Keywords.Equals(name, input.Atoms.DecimalSeparator)) { info.NumberDecimalSeparator = value; } else if (Keywords.Equals(name, input.Atoms.GroupingSeparator)) { info.NumberGroupSeparator = value; } else if (Keywords.Equals(name, input.Atoms.Infinity)) { info.PositiveInfinitySymbol = value; } else if (Keywords.Equals(name, input.Atoms.MinusSign)) { info.NegativeSign = value; } else if (Keywords.Equals(name, input.Atoms.NaN)) { info.NaNSymbol = value; } else if (Keywords.Equals(name, input.Atoms.Percent)) { info.PercentSymbol = value; } else if (Keywords.Equals(name, input.Atoms.PerMille)) { info.PerMilleSymbol = value; } else if (Keywords.Equals(name, input.Atoms.Digit)) { if (CheckAttribute(value.Length == 1, compiler)) { format.digit = value[0]; } } else if (Keywords.Equals(name, input.Atoms.ZeroDigit)) { if (CheckAttribute(value.Length == 1, compiler)) { format.zeroDigit = value[0]; } } else if (Keywords.Equals(name, input.Atoms.PatternSeparator)) { if (CheckAttribute(value.Length == 1, compiler)) { format.patternSeparator = value[0]; } } } while(input.MoveToNextAttribute()); info.NegativeInfinitySymbol = String.Concat(info.NegativeSign, info.PositiveInfinitySymbol); if (Name == null) { Name = new XmlQualifiedName(null, null); } compiler.AddDecimalFormat(Name, format); input.ToParent(); } }