PercentFormat.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.ComponentModel;
  5. using FastReport.Utils;
  6. using System.Globalization;
  7. namespace FastReport.Format
  8. {
  9. /// <summary>
  10. /// Defines how percent values are formatted and displayed.
  11. /// </summary>
  12. public class PercentFormat : FormatBase
  13. {
  14. #region Fields
  15. private bool useLocale;
  16. private int decimalDigits;
  17. private string decimalSeparator;
  18. private string groupSeparator;
  19. private string percentSymbol;
  20. private int positivePattern;
  21. private int negativePattern;
  22. #endregion
  23. #region Properties
  24. /// <summary>
  25. /// Gets or sets a value that determines whether to use system locale settings to format a value.
  26. /// </summary>
  27. [DefaultValue(true)]
  28. public bool UseLocale
  29. {
  30. get { return useLocale; }
  31. set { useLocale = value; }
  32. }
  33. /// <summary>
  34. /// Gets or sets the number of decimal places to use in percent values.
  35. /// </summary>
  36. [DefaultValue(2)]
  37. public int DecimalDigits
  38. {
  39. get { return decimalDigits; }
  40. set { decimalDigits = value; }
  41. }
  42. /// <summary>
  43. /// Gets or sets the string to use as the decimal separator in percent values.
  44. /// </summary>
  45. public string DecimalSeparator
  46. {
  47. get { return decimalSeparator; }
  48. set { decimalSeparator = value; }
  49. }
  50. /// <summary>
  51. /// Gets or sets the string that separates groups of digits to the left of the decimal in percent values.
  52. /// </summary>
  53. public string GroupSeparator
  54. {
  55. get { return groupSeparator; }
  56. set { groupSeparator = value; }
  57. }
  58. /// <summary>
  59. /// Gets or sets the string to use as the percent symbol.
  60. /// </summary>
  61. public string PercentSymbol
  62. {
  63. get { return percentSymbol; }
  64. set { percentSymbol = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the format pattern for positive percent values.
  68. /// </summary>
  69. /// <remarks>This property can have one of the values in the following table.
  70. /// The symbol "%" is the <b>PercentSymbol</b> and <i>n</i> is a number.
  71. /// <list type="table">
  72. /// <listheader><term>Value</term><description>Associated Pattern</description></listheader>
  73. /// <item><term>0</term><description>n %</description></item>
  74. /// <item><term>1</term><description>n%</description></item>
  75. /// <item><term>2</term><description>%n</description></item>
  76. /// <item><term>3</term><description>% n</description></item>
  77. /// </list>
  78. /// </remarks>
  79. [DefaultValue(0)]
  80. public int PositivePattern
  81. {
  82. get { return positivePattern; }
  83. set { positivePattern = value; }
  84. }
  85. /// <summary>
  86. /// Gets or sets the format pattern for negative percent values.
  87. /// </summary>
  88. /// <remarks>This property can have one of the values in the following table.
  89. /// The symbol "%" is the <b>PercentSymbol</b> and <i>n</i> is a number.
  90. /// <list type="table">
  91. /// <listheader><term>Value</term><description>Associated Pattern</description></listheader>
  92. /// <item><term>0</term> <description>-n %</description></item>
  93. /// <item><term>1</term> <description>-n%</description></item>
  94. /// <item><term>2</term> <description>-%n</description></item>
  95. /// <item><term>3</term> <description>%-n</description></item>
  96. /// <item><term>4</term> <description>%n-</description></item>
  97. /// <item><term>5</term> <description>n-%</description></item>
  98. /// <item><term>6</term> <description>n%-</description></item>
  99. /// <item><term>7</term> <description>-%n</description></item>
  100. /// <item><term>8</term> <description>n %-</description></item>
  101. /// <item><term>9</term> <description>% n-</description></item>
  102. /// <item><term>10</term><description>% -n</description></item>
  103. /// <item><term>11</term><description>n- %</description></item>
  104. /// </list>
  105. /// </remarks>
  106. [DefaultValue(0)]
  107. public int NegativePattern
  108. {
  109. get { return negativePattern; }
  110. set { negativePattern = value; }
  111. }
  112. #endregion
  113. #region Public Methods
  114. /// <inheritdoc/>
  115. public override FormatBase Clone()
  116. {
  117. PercentFormat result = new PercentFormat();
  118. result.UseLocale = UseLocale;
  119. result.DecimalDigits = DecimalDigits;
  120. result.DecimalSeparator = DecimalSeparator;
  121. result.GroupSeparator = GroupSeparator;
  122. result.PercentSymbol = PercentSymbol;
  123. result.PositivePattern = PositivePattern;
  124. result.NegativePattern = NegativePattern;
  125. return result;
  126. }
  127. /// <inheritdoc/>
  128. public override bool Equals(object obj)
  129. {
  130. PercentFormat f = obj as PercentFormat;
  131. return f != null &&
  132. UseLocale == f.UseLocale &&
  133. DecimalDigits == f.DecimalDigits &&
  134. DecimalSeparator == f.DecimalSeparator &&
  135. GroupSeparator == f.GroupSeparator &&
  136. PercentSymbol == f.PercentSymbol &&
  137. PositivePattern == f.PositivePattern &&
  138. NegativePattern == f.NegativePattern;
  139. }
  140. /// <inheritdoc/>
  141. public override int GetHashCode()
  142. {
  143. return base.GetHashCode();
  144. }
  145. /// <inheritdoc/>
  146. public override string FormatValue(object value)
  147. {
  148. if (value is Variant)
  149. value = ((Variant)value).Value;
  150. return String.Format(GetNumberFormatInfo(), "{0:p}", new object[] { value });
  151. }
  152. internal NumberFormatInfo GetNumberFormatInfo()
  153. {
  154. NumberFormatInfo info = new NumberFormatInfo();
  155. if (UseLocale)
  156. {
  157. NumberFormatInfo cultureFormat = CultureInfo.CurrentCulture.NumberFormat;
  158. info.PercentDecimalDigits = DecimalDigits;
  159. info.PercentDecimalSeparator = cultureFormat.PercentDecimalSeparator;
  160. info.PercentGroupSizes = cultureFormat.PercentGroupSizes;
  161. info.PercentGroupSeparator = cultureFormat.PercentGroupSeparator;
  162. info.PercentSymbol = cultureFormat.PercentSymbol;
  163. info.PercentPositivePattern = cultureFormat.PercentPositivePattern;
  164. info.PercentNegativePattern = cultureFormat.PercentNegativePattern;
  165. }
  166. else
  167. {
  168. info.PercentDecimalDigits = DecimalDigits;
  169. info.PercentDecimalSeparator = DecimalSeparator;
  170. info.PercentGroupSizes = new int[] { 3 };
  171. info.PercentGroupSeparator = GroupSeparator;
  172. info.PercentSymbol = PercentSymbol;
  173. info.PercentPositivePattern = PositivePattern;
  174. info.PercentNegativePattern = NegativePattern;
  175. }
  176. return info;
  177. }
  178. internal override string GetSampleValue()
  179. {
  180. return FormatValue(1.23f);
  181. }
  182. internal override void Serialize(FRWriter writer, string prefix, FormatBase format)
  183. {
  184. base.Serialize(writer, prefix, format);
  185. PercentFormat c = format as PercentFormat;
  186. if (c == null || UseLocale != c.UseLocale)
  187. writer.WriteBool(prefix + "UseLocale", UseLocale);
  188. if (c == null || DecimalDigits != c.DecimalDigits)
  189. writer.WriteInt(prefix + "DecimalDigits", DecimalDigits);
  190. if (!UseLocale)
  191. {
  192. if (c == null || DecimalSeparator != c.DecimalSeparator)
  193. writer.WriteStr(prefix + "DecimalSeparator", DecimalSeparator);
  194. if (c == null || GroupSeparator != c.GroupSeparator)
  195. writer.WriteStr(prefix + "GroupSeparator", GroupSeparator);
  196. if (c == null || PercentSymbol != c.PercentSymbol)
  197. writer.WriteStr(prefix + "PercentSymbol", PercentSymbol);
  198. if (c == null || PositivePattern != c.PositivePattern)
  199. writer.WriteInt(prefix + "PositivePattern", PositivePattern);
  200. if (c == null || NegativePattern != c.NegativePattern)
  201. writer.WriteInt(prefix + "NegativePattern", NegativePattern);
  202. }
  203. }
  204. #endregion
  205. /// <summary>
  206. /// Initializes a new instance of the <b>PercentFormat</b> class with default settings.
  207. /// </summary>
  208. public PercentFormat()
  209. {
  210. UseLocale = true;
  211. DecimalDigits = 2;
  212. DecimalSeparator = ".";
  213. GroupSeparator = ",";
  214. PercentSymbol = "%";
  215. }
  216. }
  217. }