Skip to content

Commit

Permalink
Merge pull request #307 from dmitry-gokun/master
Browse files Browse the repository at this point in the history
string.FormatWith: added support for the explicit culture parameter.
  • Loading branch information
MehdiK committed Jul 2, 2014
2 parents cd1ae3d + 1643fdc commit 9db15c0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,14 @@ This is an extension method based on `String.Format`, so exact rules applies to
If `format` is null, it'll throw `ArgumentNullException`.
If passed a fewer number for arguments, it'll throw `String.FormatException` exception.

You also can specify the culture to use explicitly as the first parameter for the `FormatWith()` method:

```c#
"{0:N2}".FormatWith(new CultureInfo("ru-RU"), 6666.66) => "6 666,66"
```

If a culture is not specified, current thread's current culture is used.

###<a id="humanize-enums">Humanize Enums</a>
Calling `ToString` directly on enum members usually results in less than ideal output for users. The solution to this is usually to use `DescriptionAttribute` data annotation and then read that at runtime to get a more friendly output. That is a great solution; but more often than not we only need to put some space between words of an enum member - which is what `String.Humanize()` does well. For an enum like:

Expand Down
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
###In Development
- [#306](https://github.com/MehdiK/Humanizer/pull/306): Added Singularize/Pluralize overload without using obsolete plurality enum
- [#303](https://github.com/MehdiK/Humanizer/pull/303): Added support for all integer types in ByteSize extensions
- [#307](https://github.com/MehdiK/Humanizer/pull/307): string.FormatWith: added support for the explicit culture parameter.

[Commits](https://github.com/MehdiK/Humanizer/compare/v1.27.0...master)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,7 @@ public class StringDehumanizeExtensions
public class StringExentions
{
public string FormatWith(string format, object[] args) { }
public string FormatWith(string format, System.IFormatProvider provider, object[] args) { }
}

public class StringHumanizeExtensions
Expand Down
12 changes: 11 additions & 1 deletion src/Humanizer.Tests/StringExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Globalization;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests
{
Expand Down Expand Up @@ -32,5 +34,13 @@ public void FormatCannotBeNull()
string format = null;
Assert.Throws<ArgumentNullException>(() => format.FormatWith(1, 2));
}

[Theory]
[InlineData("en-US", "6,666.66")]
[InlineData("ru-RU", "6 666,66")]
public void CanSpecifyCultureExplicitly(string culture, string expected)
{
Assert.Equal(expected, "{0:N2}".FormatWith(new CultureInfo(culture), 6666.66));
}
}
}
}
14 changes: 13 additions & 1 deletion src/Humanizer/StringExentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Humanizer
public static class StringExentions
{
/// <summary>
/// Extension method to format string with passed arguments
/// Extension method to format string with passed arguments. Current thread's current culture is used
/// </summary>
/// <param name="format">string format</param>
/// <param name="args">arguments</param>
Expand All @@ -17,5 +17,17 @@ public static string FormatWith(this string format, params object[] args)
{
return String.Format(format, args);
}

/// <summary>
/// Extension method to format string with passed arguments using specified format provider (i.e. CultureInfo)
/// </summary>
/// <param name="format">string format</param>
/// <param name="provider">An object that supplies culture-specific formatting information</param>
/// <param name="args">arguments</param>
/// <returns></returns>
public static string FormatWith(this string format, IFormatProvider provider, params object[] args)
{
return String.Format(provider, format, args);
}
}
}

0 comments on commit 9db15c0

Please sign in to comment.