Support for globalization.

Jan 13, 2009 at 8:45 PM
Awsome library, btw. 

I was working with your Utilities.Miscellaneous.CurrencyUtils and realized the current implementations didn't work for non-US implementations.

In particular you may want to consider something like this:
   
    using System.Globalization;

    ...

    public static decimal ConvertToDecimal(string value)
    {
        return ConvertToDecimal(value, CultureInfo.CurrentCulture);
    }

    public static decimal ConvertToDecimal(string value, CultureInfo culture)
    {
        ValidationUtils.ArgumentNotNullOrEmpty(value, "value");
        ValidationUtils.ArgumentNotNull(culture, "culture");

        // remove whitespace
        string trimmedValue = value.Trim();

        // lookup the symbol for the culture
        string symbol = culture.NumberFormat.CurrencySymbol;

        // remove leading symbol
        trimmedValue = trimmedValue.TrimStart(symbol[0]);

        return Convert.ToDecimal(trimmedValue);
    }

I'm working on the correct ValidateCurrencyString as well, but am rusty on my RegEx and would probably have to use a readonly property instead of a const to get the mechanics for the dynamic lookup of the Currency Symbol.

Jan 13, 2009 at 8:56 PM
Edited Jan 13, 2009 at 9:03 PM
Well I had a little more time to work out the regular expression part of the module:

    private const string VALIDATE_CURRENCTY_TEMPLATE = @"(^{0}?(?!0,?\d)\d{1,3}(,?\d{3})*(\.\d\d)?)$";
    private static string _ValidateCurrencyString = "";
    public static string ValidateCurrencyString
    {
        get
        {
            return _ValidateCurrencyString;            
        }
    }

    static CurrencyUtils()
    {
        _ValidateCurrencyString = GetCurrencyValidationString(CultureInfo.CurrentCulture);
    }

    public static string GetCurrencyValidationString(CultureInfo culture)
    {
        string symbol = Regex.Escape(CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol);
        return string.Format(VALIDATE_CURRENCTY_TEMPLATE, symbol);
    }