Files
ShrlAlgoToolkit/WPFluent/Controls/NumberBox/ValidateNumberFormatter.cs

40 lines
1.4 KiB
C#

// This Source Code is partially based on the source code provided by the .NET Foundation.
// ReSharper disable once CheckNamespace
namespace WPFluent.Controls;
/// <summary>
/// Base nubmer formatter that uses default format specifier and <see cref="CultureInfo"/> that represents the culture
/// used by the current thread.
/// </summary>
public class ValidateNumberFormatter : INumberFormatter, INumberParser
{
private static CultureInfo GetCurrentCultureConverter() { return CultureInfo.CurrentCulture; }
private static string GetFormatSpecifier() { return "G"; }
/// <inheritdoc/>
public string FormatDouble(double? value)
{ return value?.ToString(GetFormatSpecifier(), GetCurrentCultureConverter()) ?? string.Empty; }
/// <inheritdoc/>
public string FormatInt(int? value)
{ return value?.ToString(GetFormatSpecifier(), GetCurrentCultureConverter()) ?? string.Empty; }
/// <inheritdoc/>
public string FormatUInt(uint? value)
{ return value?.ToString(GetFormatSpecifier(), GetCurrentCultureConverter()) ?? string.Empty; }
/// <inheritdoc/>
public double? ParseDouble(string value) { return double.TryParse(value, out var d) ? d : null; }
/// <inheritdoc/>
public int? ParseInt(string value) { return int.TryParse(value, out var i) ? i : null; }
/// <inheritdoc/>
public uint? ParseUInt(string value) { return uint.TryParse(value, out var ui) ? ui : null; }
}