Skip to content

Commit

Permalink
Added Standard Money Format 'I' to display code instead of symbol #46
Browse files Browse the repository at this point in the history
  • Loading branch information
remyvd committed Sep 11, 2016
1 parent 729fc01 commit c72cf84
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 60 deletions.
1 change: 1 addition & 0 deletions NodaMoney.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
build\default.ps1 = build\default.ps1
global.json = global.json
src\GlobalAssemblyInfo.cs = src\GlobalAssemblyInfo.cs
README.md = README.md
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "NodaMoney", "src\NodaMoney\NodaMoney.xproj", "{58F0FE5E-E139-4ABF-B9A5-969DBBE69F52}"
Expand Down
67 changes: 58 additions & 9 deletions src/NodaMoney/Money.Formattable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public string ToString(string format, IFormatProvider formatProvider)
return ConvertToString(format, formatProvider);
}

private static NumberFormatInfo GetNumberFormatInfo(Currency currency, IFormatProvider formatProvider)
private static IFormatProvider GetFormatProvider(Currency currency, IFormatProvider formatProvider, bool useCode = false)
{
var cc = CultureInfo.CurrentCulture;

Expand All @@ -73,21 +73,70 @@ private static NumberFormatInfo GetNumberFormatInfo(Currency currency, IFormatPr
if (nfi != null)
numberFormatInfo = (NumberFormatInfo)nfi.Clone();
}

numberFormatInfo.CurrencySymbol = currency.Symbol;

numberFormatInfo.CurrencyDecimalDigits = (int)currency.DecimalDigits;
numberFormatInfo.CurrencySymbol = currency.Symbol;

if (useCode)
{
// Replace symbol with the code
numberFormatInfo.CurrencySymbol = currency.Code;

// Add spacing to PositivePattern and NegativePattern
if (numberFormatInfo.CurrencyPositivePattern == 0) // $n
numberFormatInfo.CurrencyPositivePattern = 2; // $ n
if (numberFormatInfo.CurrencyPositivePattern == 1) // n$
numberFormatInfo.CurrencyPositivePattern = 3; // n $

switch (numberFormatInfo.CurrencyNegativePattern)
{
case 0: // ($n)
numberFormatInfo.CurrencyNegativePattern = 14; // ($ n)
break;
case 1: // -$n
numberFormatInfo.CurrencyNegativePattern = 9; // -$ n
break;
case 2: // $-n
numberFormatInfo.CurrencyNegativePattern = 12; // $ -n
break;
case 3: // $n-
numberFormatInfo.CurrencyNegativePattern = 11; // $ n-
break;
case 4: // (n$)
numberFormatInfo.CurrencyNegativePattern = 15; // (n $)
break;
case 5: // -n$
numberFormatInfo.CurrencyNegativePattern = 8; // -n $
break;
case 6: // n-$
numberFormatInfo.CurrencyNegativePattern = 13; // n- $
break;
case 7: // n$-
numberFormatInfo.CurrencyNegativePattern = 10; // n $-
break;
}
}

return numberFormatInfo;
}

private string ConvertToString(string format, IFormatProvider formatProvider)
{
// TODO: ICustomFormat : http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.IFormatProvider);k(TargetFrameworkMoniker-.NETPortable,Version%3Dv4.6);k(DevLang-csharp)&rd=true
// TODO: Move to Currency? Currency.GetNumberFormatInfo()
// TODO: Add custom format to represent USD 12.34, EUR 12.35, etc.
// The formatting of Money should respect the NumberFormat of the current Culture, except for the CurrencySymbol and CurrencyDecimalDigits.
// http://en.wikipedia.org/wiki/Linguistic_issues_concerning_the_euro
// TODO: ICustomFormat : http://msdn.microsoft.com/query/dev12.query?appId=Dev12IDEF1&l=EN-US&k=k(System.IFormatProvider);k(TargetFrameworkMoniker-.NETPortable,Version%3Dv4.6);k(DevLang-csharp)&rd=true
// TODO: Hacked solution, solve with better implementation

IFormatProvider provider;
if (!string.IsNullOrWhiteSpace(format) && format.StartsWith("I") && format.Length >= 1 && format.Length <= 2)
{
format = format.Replace("I", "C");
provider = GetFormatProvider(Currency, formatProvider, true);
}
else
{
provider = GetFormatProvider(Currency, formatProvider);
}

return Amount.ToString(format ?? "C", GetNumberFormatInfo(Currency, formatProvider));
return Amount.ToString(format ?? "C", provider);
}
}
}
12 changes: 6 additions & 6 deletions src/NodaMoney/Money.Parsable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static Money Parse(string value)

Currency currency = ExtractCurrencyFromString(value);

return Parse(value, NumberStyles.Currency, GetNumberFormatInfo(currency, null), currency);
return Parse(value, NumberStyles.Currency, GetFormatProvider(currency, null), currency);
}

/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent.</summary>
Expand All @@ -37,7 +37,7 @@ public static Money Parse(string value, Currency currency)
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(value));

return Parse(value, NumberStyles.Currency, GetNumberFormatInfo(currency, null), currency);
return Parse(value, NumberStyles.Currency, GetFormatProvider(currency, null), currency);
}

/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent.</summary>
Expand All @@ -54,7 +54,7 @@ public static Money Parse(string value, NumberStyles style, IFormatProvider prov
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentNullException(nameof(value));

decimal amount = decimal.Parse(value, style, GetNumberFormatInfo(currency, provider));
decimal amount = decimal.Parse(value, style, GetFormatProvider(currency, provider));
return new Money(amount, currency);
}

Expand Down Expand Up @@ -86,7 +86,7 @@ public static bool TryParse(string value, out Money result)
return false;
}

return TryParse(value, NumberStyles.Currency, GetNumberFormatInfo(currency, null), currency, out result);
return TryParse(value, NumberStyles.Currency, GetFormatProvider(currency, null), currency, out result);
}

/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent. A return value indicates whether the conversion succeeded or failed.</summary>
Expand All @@ -101,7 +101,7 @@ public static bool TryParse(string value, out Money result)
/// <remarks>See <see cref="Decimal.TryParse(String, out Decimal)"/> for more info and remarks.</remarks>
public static bool TryParse(string value, Currency currency, out Money result)
{
return TryParse(value, NumberStyles.Currency, GetNumberFormatInfo(currency, null), currency, out result);
return TryParse(value, NumberStyles.Currency, GetFormatProvider(currency, null), currency, out result);
}

/// <summary>Converts the string representation of a money value to its <see cref="Money"/> equivalent. A return value indicates whether the conversion succeeded or failed.</summary>
Expand All @@ -119,7 +119,7 @@ public static bool TryParse(string value, Currency currency, out Money result)
public static bool TryParse(string value, NumberStyles style, IFormatProvider provider, Currency currency, out Money result)
{
decimal amount;
bool isParsingSuccessful = decimal.TryParse(value, style, GetNumberFormatInfo(currency, provider), out amount);
bool isParsingSuccessful = decimal.TryParse(value, style, GetFormatProvider(currency, provider), out amount);

if (isParsingSuccessful)
{
Expand Down
Loading

0 comments on commit c72cf84

Please sign in to comment.