Skip to content

Commit

Permalink
- added new FormatAsCurrency lava filter which will use the currency …
Browse files Browse the repository at this point in the history
…symbol from the CurrencySymbol Global Attribute

- added int? and int to the FormatAsCurrency extensions
  • Loading branch information
MikeDPeterson committed Aug 27, 2015
1 parent 9468dcc commit f8e0e2f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Rock/Lava/RockFilters.cs
Expand Up @@ -912,6 +912,32 @@ public static string Format( object input, string format )
return string.Format( "{0:" + format + "}", input );
}

/// <summary>
/// Formats the specified input as currency using the CurrencySymbol from Global Attributes
/// </summary>
/// <param name="input">The input.</param>
/// <returns></returns>
public static string FormatAsCurrency( object input )
{
if ( input == null )
{
return null;
}

if (input is string)
{
// if the input is a string, just append the currency symbol to the front, even if it can't be converted to a number
var currencySymbol = GlobalAttributesCache.Value( "CurrencySymbol" );
return string.Format("{0}{1}", currencySymbol, input);
}
else
{
// if the input an integer, decimal, double or anything else that can be parsed as a decimal, format that
decimal? inputAsDecimal = input.ToString().AsDecimalOrNull();
return inputAsDecimal.FormatAsCurrency();
}
}

/// <summary>
/// Addition - Overriding this to change the logic. The default filter will concat if the type is
/// string. This one does the math if the input can be parsed as a int
Expand Down
22 changes: 21 additions & 1 deletion Rock/Utility/ExtensionMethods/CurrencyExtensions.cs
Expand Up @@ -32,7 +32,7 @@ public static partial class ExtensionMethods
/// <returns></returns>
public static string FormatAsCurrency( this decimal value )
{
var currencySymbol = GlobalAttributesCache.Value( "CurrencySymbol " );
var currencySymbol = GlobalAttributesCache.Value( "CurrencySymbol" );
return string.Format( "{0}{1:N}", currencySymbol, value );
}

Expand Down Expand Up @@ -73,6 +73,26 @@ public static string FormatAsCurrency( this double? value )
return ( (decimal?)value ).FormatAsCurrency();
}

/// <summary>
/// Formats as currency using the CurrencySymbol from Global Attributes
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string FormatAsCurrency( this int value )
{
return ( (decimal)value ).FormatAsCurrency();
}

/// <summary>
/// Formats as currency using the CurrencySymbol from Global Attributes
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string FormatAsCurrency( this int? value )
{
return ( (decimal?)value ).FormatAsCurrency();
}

#endregion
}
}

0 comments on commit f8e0e2f

Please sign in to comment.