-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Corniel Nobel
committed
May 31, 2022
1 parent
292c749
commit 8e94c0d
Showing
3 changed files
with
57 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
namespace Qowaiv.Financial; | ||
|
||
|
||
public partial struct Amount | ||
#if NET6_0_OR_GREATER | ||
: ISpanFormattable | ||
#endif | ||
{ | ||
|
||
#if NET6_0_OR_GREATER | ||
|
||
[Pure] | ||
public string ToString(string? format, IFormatProvider? formatProvider) | ||
{ | ||
var span = new Span<char>(new char[128]); | ||
((ISpanFormattable)this).TryFormat(span, out var size, format, formatProvider); | ||
return span[..size].ToString(); | ||
} | ||
|
||
/// <inheritdoc /> | ||
bool ISpanFormattable.TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider) | ||
{ | ||
if (StringFormatter.TryApplyCustomFormatter(format.ToString(), this, provider, out string formatted)) | ||
{ | ||
formatted.CopyTo(destination); | ||
charsWritten = formatted.Length; | ||
return true; | ||
} | ||
else return m_Value.TryFormat(destination, out charsWritten, format, Money.GetNumberFormatInfo(provider)); | ||
} | ||
#else | ||
|
||
/// <summary>Returns a formatted <see cref="string"/> that represents the current </summary> | ||
/// <param name="format"> | ||
/// The format that describes the formatting. | ||
/// </param> | ||
/// <param name="formatProvider"> | ||
/// The format provider. | ||
/// </param> | ||
[Pure] | ||
public string ToString(string? format, IFormatProvider? formatProvider) | ||
=> StringFormatter.TryApplyCustomFormatter(format, this, formatProvider, out string formatted) | ||
? formatted | ||
: m_Value.ToString(format, Money.GetNumberFormatInfo(formatProvider)); | ||
|
||
#endif | ||
} |