Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string.FormatWith: added support for the explicit culture parameter. #307

Merged
merged 2 commits into from
Jul 2, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding readme notes.


```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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please change this so it's consistent with other notes? e.g. Added support for ...


[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);
}
}
}