| | 1 | | using System.Globalization; |
| | 2 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 3 | | using ValidateLib.Metadata.Descriptors; |
| | 4 | |
|
| | 5 | | namespace ValidateLib.TabularData.Datatypes.NumericDatatypes |
| | 6 | | { |
| | 7 | | public class FloatDT : FloatBaseDT |
| | 8 | | { |
| 1 | 9 | | protected override string pattern { get; set; } = |
| 1 | 10 | | @"^(?:\+|-)?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[Ee](?:\+|-)?[0-9]+)?|(?:\+|-)?INF|NaN$"; |
| 1 | 11 | | new public float Value { get; set; } |
| | 12 | |
|
| 1 | 13 | | public FloatDT() { } |
| 1 | 14 | | public FloatDT(string strValue) : base(strValue) |
| | 15 | | { |
| 1 | 16 | | CheckLexicalSpace(strValue); |
| | 17 | | try |
| | 18 | | { |
| 1 | 19 | | if (HandleSpecialCases(strValue)) |
| 1 | 20 | | Value = float.Parse(stringValue, CultureInfo.InvariantCulture); |
| 1 | 21 | | ResolvePercentPermileExponent(); |
| 1 | 22 | | } |
| 1 | 23 | | catch (Exception) |
| | 24 | | { |
| 1 | 25 | | ErrorFactory.ThrowDatatypeValidationError(strValue); |
| 0 | 26 | | } |
| 1 | 27 | | } |
| | 28 | |
|
| 0 | 29 | | public FloatDT(string strValue, FormatDescriptor format) : base(strValue, format) |
| | 30 | | { |
| 0 | 31 | | HandleSpecialCases(strValue); |
| | 32 | | try |
| | 33 | | { |
| | 34 | | checked |
| | 35 | | { |
| 0 | 36 | | Value = (float)base.Value; |
| 0 | 37 | | ResolvePercentPermileExponent(); |
| | 38 | | } |
| 0 | 39 | | } |
| 0 | 40 | | catch (Exception) |
| | 41 | | { |
| 0 | 42 | | ErrorFactory.ThrowDatatypeValidationError(strValue); |
| 0 | 43 | | } |
| | 44 | |
|
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | |
|
| | 48 | | protected bool HandleSpecialCases(string strValue) |
| | 49 | | { |
| 1 | 50 | | if (stringValue == "NaN") |
| | 51 | | { |
| 1 | 52 | | Value = float.NaN; |
| 1 | 53 | | return false; |
| | 54 | | } |
| 1 | 55 | | if (stringValue == "-INF") |
| | 56 | | { |
| 1 | 57 | | Value = float.NegativeInfinity; |
| 1 | 58 | | return false; |
| | 59 | | } |
| 1 | 60 | | if (stringValue == "INF") |
| | 61 | | { |
| 1 | 62 | | Value = float.PositiveInfinity; |
| 1 | 63 | | return false; |
| | 64 | | } |
| 1 | 65 | | return true; |
| | 66 | | } |
| | 67 | |
|
| | 68 | |
|
| | 69 | | override public int CompareTo(BaseDT? other) |
| | 70 | | { |
| 1 | 71 | | if (other is null) |
| 0 | 72 | | return 1; |
| 1 | 73 | | if (other is not FloatDT) |
| 0 | 74 | | throw new ArgumentException(); |
| 1 | 75 | | var typedOther = (FloatDT)other; |
| 1 | 76 | | return Value.CompareTo(typedOther.Value); |
| | 77 | |
|
| | 78 | | } |
| | 79 | | } |
| | 80 | | } |