Skip to content

Commit

Permalink
It works, but does not scale yet.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel Nobel committed May 31, 2022
1 parent 292c749 commit 8e94c0d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
10 changes: 10 additions & 0 deletions specs/Qowaiv.Specs/Financial/Amount_specs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ public void to_divide_amount_by_amount_as_decimal()
}
}
}

public class Supports_try_format
{
[Test]
public void On_interpolated_strings()
{
FormattableString.Invariant($"{Svo.Amount}").Should().Be("42.17");
}

}
13 changes: 0 additions & 13 deletions src/Qowaiv/Financial/Amount.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,19 +375,6 @@ namespace Qowaiv.Financial;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private string DebuggerDisplay => this.DebuggerDisplay("¤{0:0.00########}");

/// <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));

/// <summary>Gets an XML string representation of the amount.</summary>
[Pure]
private string ToXmlString() => ToString(CultureInfo.InvariantCulture);
Expand Down
47 changes: 47 additions & 0 deletions src/Qowaiv/Financial/Amount.experimental.cs
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
}

0 comments on commit 8e94c0d

Please sign in to comment.