using System;
using System.Globalization;
namespace Ubisoft
{
///
/// Utility class for dealing with float type.
///
public static class HtFloat
{
public static readonly IFormatProvider DefaultFormatProvider = HtTypes.DefaultFormatProvider;
///
/// Converts the string representation of a number to its single-precision floating-point number equivalent.
/// A return value indicates whether the conversion succeeded or failed.
///
/// This method uses NumberStyles.Float and CultureInfo.InvariantCulture.
/// Use this method when the string to convert comes from Content or persistence.
///
/// A string containing a number to convert.
/// When this method returns, contains single-precision floating-point number equivalent to the numeric of the s parameter,
/// if the conversion succeeded, or zero if the conversion failed.
/// The conversion fails if the s parameter is null or Empty or is not a number in a valid format.
/// It also fails on .NET Framework and .NET Core 2.2 and earlier versions if s represents a number less than MinValue or
/// greater than MaxValue.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
///
/// true if s was converted successfully; otherwise, false
public static bool TryParse(string s, out float result)
{
return TryParse(s, DefaultFormatProvider, out result);
}
///
/// Converts the string representation of a number to its single-precision floating-point number equivalent.
/// A return value indicates whether the conversion succeeded or failed.
///
/// This method uses NumberStyles.Float.
///
/// A string containing a number to convert.
/// An object that supplies culture-specific formatting information about s.
/// When this method returns, contains the single-precision floating-point number equivalent of the s parameter,
/// if the conversion succeeded, or zero if the conversion failed.
/// The conversion fails if the s parameter is null or Empty or is not a number in a valid format.
/// It also fails on .NET Framework and .NET Core 2.2 and earlier versions if s represents a number less than MinValue or
/// greater than MaxValue.
/// This parameter is passed uninitialized; any value originally supplied in result will be overwritten.
///
/// true if s was converted successfully; otherwise, false
public static bool TryParse(string s, IFormatProvider provider, out float result)
{
return float.TryParse(s, NumberStyles.Float, provider, out result);
}
///
/// Converts the string representation of a number to its single-precision floating-point number equivalent.
///
/// This method uses NumberStyles.Float and CultureInfo.InvariantCulture.
/// Use this method when the string to convert comes from Content or persistence.
/// A string containing a number to convert.
/// A single-precision floating-point number equivalent to the numeric value or symbol specified in s.
/// or
/// ArgumentNullException if s is null.
/// or
/// FormatException if s is not in a format compliant with CultureInfo.InvariantCulture.
/// or
/// OverflowException if s represents a number less than MinValue or greater than MaxValue.
///
public static float Parse(string s)
{
return Parse(s, DefaultFormatProvider);
}
///
/// Converts the string representation of a number to its single-precision floating-point number equivalent.
///
/// This method uses NumberStyles.Float.
/// A string containing a number to convert.
/// An object that supplies culture-specific formatting information about s.
/// A single-precision floating-point number equivalent to the numeric value or symbol specified in s.
/// or
/// ArgumentNullException if s is null.
/// or
/// FormatException if s is not in a format compliant with CultureInfo.InvariantCulture.
/// or
/// OverflowException if s represents a number less than MinValue or greater than MaxValue.
///
public static float Parse(string s, IFormatProvider provider)
{
return float.Parse(s, NumberStyles.Float, provider);
}
///
/// Converts the numeric value of this instance to its equivalent string representation using CultureInfo.InvariantCulture.
///
/// Use this method when you need the string returned to be independent from user's culture, typically when storing persistence, which
/// will be loaded by using HtFloat.Parse() or HtFloat.TryParse() methods.
///
/// A single-precision floating-point number.
/// The string representation of the value.
public static string ToString(float value)
{
return value.ToString(DefaultFormatProvider);
}
}
}