CurrencyFormat.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 currency values are formatted and displayed.
  11. /// </summary>
  12. public class CurrencyFormat : FormatBase
  13. {
  14. #region Fields
  15. private bool useLocale;
  16. private int decimalDigits;
  17. private string decimalSeparator;
  18. private string groupSeparator;
  19. private string currencySymbol;
  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 currency 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 currency 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 currency 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 currency symbol.
  60. /// </summary>
  61. public string CurrencySymbol
  62. {
  63. get { return currencySymbol; }
  64. set { currencySymbol = value; }
  65. }
  66. /// <summary>
  67. /// Gets or sets the format pattern for positive currency values.
  68. /// </summary>
  69. /// <remarks>This property can have one of the values in the following table.
  70. /// The symbol "$" is the <b>CurrencySymbol</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 currency values.
  87. /// </summary>
  88. /// <remarks>This property can have one of the values in the following table.
  89. /// The symbol "$" is the <b>CurrencySymbol</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. /// <item><term>12</term><description>$ -n</description></item>
  105. /// <item><term>13</term><description>n- $</description></item>
  106. /// <item><term>14</term><description>($ n)</description></item>
  107. /// <item><term>15</term><description>(n $)</description></item>
  108. /// </list>
  109. /// </remarks>
  110. [DefaultValue(0)]
  111. public int NegativePattern
  112. {
  113. get { return negativePattern; }
  114. set { negativePattern = value; }
  115. }
  116. #endregion
  117. #region Public Methods
  118. /// <inheritdoc/>
  119. public override FormatBase Clone()
  120. {
  121. CurrencyFormat result = new CurrencyFormat();
  122. result.UseLocale = UseLocale;
  123. result.DecimalDigits = DecimalDigits;
  124. result.DecimalSeparator = DecimalSeparator;
  125. result.GroupSeparator = GroupSeparator;
  126. result.CurrencySymbol = CurrencySymbol;
  127. result.PositivePattern = PositivePattern;
  128. result.NegativePattern = NegativePattern;
  129. return result;
  130. }
  131. /// <inheritdoc/>
  132. public override bool Equals(object obj)
  133. {
  134. CurrencyFormat f = obj as CurrencyFormat;
  135. return f != null &&
  136. UseLocale == f.UseLocale &&
  137. DecimalDigits == f.DecimalDigits &&
  138. DecimalSeparator == f.DecimalSeparator &&
  139. GroupSeparator == f.GroupSeparator &&
  140. CurrencySymbol == f.CurrencySymbol &&
  141. PositivePattern == f.PositivePattern &&
  142. NegativePattern == f.NegativePattern;
  143. }
  144. /// <inheritdoc/>
  145. public override int GetHashCode()
  146. {
  147. return base.GetHashCode();
  148. }
  149. /// <inheritdoc/>
  150. public override string FormatValue(object value)
  151. {
  152. if (value is Variant)
  153. value = ((Variant)value).Value;
  154. return String.Format(GetNumberFormatInfo(), "{0:c}", new object[] { value });
  155. }
  156. internal NumberFormatInfo GetNumberFormatInfo()
  157. {
  158. NumberFormatInfo info = new NumberFormatInfo();
  159. if (UseLocale)
  160. {
  161. NumberFormatInfo cultureFormat = CultureInfo.CurrentCulture.NumberFormat;
  162. info.CurrencyDecimalDigits = DecimalDigits;
  163. info.CurrencyDecimalSeparator = cultureFormat.CurrencyDecimalSeparator;
  164. info.CurrencyGroupSizes = cultureFormat.CurrencyGroupSizes;
  165. info.CurrencyGroupSeparator = cultureFormat.CurrencyGroupSeparator;
  166. info.CurrencySymbol = cultureFormat.CurrencySymbol;
  167. info.CurrencyPositivePattern = cultureFormat.CurrencyPositivePattern;
  168. info.CurrencyNegativePattern = cultureFormat.CurrencyNegativePattern;
  169. }
  170. else
  171. {
  172. info.CurrencyDecimalDigits = DecimalDigits;
  173. info.CurrencyDecimalSeparator = DecimalSeparator;
  174. info.CurrencyGroupSizes = new int[] { 3 };
  175. info.CurrencyGroupSeparator = GroupSeparator;
  176. info.CurrencySymbol = CurrencySymbol;
  177. info.CurrencyPositivePattern = PositivePattern;
  178. info.CurrencyNegativePattern = NegativePattern;
  179. }
  180. return info;
  181. }
  182. internal override string GetSampleValue()
  183. {
  184. return FormatValue(-12345);
  185. }
  186. internal override void Serialize(FRWriter writer, string prefix, FormatBase format)
  187. {
  188. base.Serialize(writer, prefix, format);
  189. CurrencyFormat c = format as CurrencyFormat;
  190. if (c == null || UseLocale != c.UseLocale)
  191. writer.WriteBool(prefix + "UseLocale", UseLocale);
  192. if (c == null || DecimalDigits != c.DecimalDigits)
  193. writer.WriteInt(prefix + "DecimalDigits", DecimalDigits);
  194. if (!UseLocale)
  195. {
  196. if (c == null || DecimalSeparator != c.DecimalSeparator)
  197. writer.WriteStr(prefix + "DecimalSeparator", DecimalSeparator);
  198. if (c == null || GroupSeparator != c.GroupSeparator)
  199. writer.WriteStr(prefix + "GroupSeparator", GroupSeparator);
  200. if (c == null || CurrencySymbol != c.CurrencySymbol)
  201. writer.WriteStr(prefix + "CurrencySymbol", CurrencySymbol);
  202. if (c == null || PositivePattern != c.PositivePattern)
  203. writer.WriteInt(prefix + "PositivePattern", PositivePattern);
  204. if (c == null || NegativePattern != c.NegativePattern)
  205. writer.WriteInt(prefix + "NegativePattern", NegativePattern);
  206. }
  207. }
  208. #endregion
  209. /// <summary>
  210. /// Initializes a new instance of the <b>CurrencyFormat</b> class with default settings.
  211. /// </summary>
  212. public CurrencyFormat()
  213. {
  214. UseLocale = true;
  215. DecimalDigits = 2;
  216. DecimalSeparator = ".";
  217. GroupSeparator = ",";
  218. CurrencySymbol = "$";
  219. }
  220. }
  221. }