Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Data/3.3/DoubleValue.cs @ 12431

Last change on this file since 12431 was 12431, checked in by pfleck, 9 years ago

#2395 Fixed parsing issues by using the G17 format.

File size: 3.4 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2]4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[3306]23using System.Drawing;
[2]24using System.Text;
[3376]25using HeuristicLab.Common;
[2]26using HeuristicLab.Core;
[1853]27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]28
29namespace HeuristicLab.Data {
[3048]30  [Item("DoubleValue", "Represents a double value.")]
[3017]31  [StorableClass]
[3054]32  public class DoubleValue : ValueTypeValue<double>, IComparable, IStringConvertibleValue {
[7201]33    public static new Image StaticItemImage {
[5287]34      get { return HeuristicLab.Common.Resources.VSImageLibrary.Field; }
[3306]35    }
36
[4722]37    [StorableConstructor]
38    protected DoubleValue(bool deserializing) : base(deserializing) { }
39    protected DoubleValue(DoubleValue original, Cloner cloner)
40      : base(original, cloner) {
41    }
[3048]42    public DoubleValue() : base() { }
43    public DoubleValue(double value) : base(value) { }
[2]44
[2665]45    public override IDeepCloneable Clone(Cloner cloner) {
[4722]46      return new DoubleValue(this, cloner);
[2]47    }
[2665]48
[2669]49    public override string ToString() {
[12431]50      return Value.ToString("G17");  // round-trip format
51      // Note that the round-trip format "r" can still lead to different values when run on an 64-bit machine, therefore using G17 instead.
52      // (https://msdn.microsoft.com/en-us/library/kfsatb94.aspx)
[2669]53    }
54
[3054]55    public virtual int CompareTo(object obj) {
[3048]56      DoubleValue other = obj as DoubleValue;
[2757]57      if (other != null)
58        return Value.CompareTo(other.Value);
59      else
60        return Value.CompareTo(obj);
61    }
62
[3054]63    protected virtual bool Validate(string value, out string errorMessage) {
[2694]64      double val;
65      bool valid = double.TryParse(value, out val);
66      errorMessage = string.Empty;
67      if (!valid) {
68        StringBuilder sb = new StringBuilder();
69        sb.Append("Invalid Value (Valid Value Format: \"");
70        sb.Append(FormatPatterns.GetDoubleFormatPattern());
71        sb.Append("\")");
72        errorMessage = sb.ToString();
73      }
74      return valid;
[2676]75    }
[3054]76    protected virtual string GetValue() {
[12431]77      return ToString();
[2665]78    }
[3054]79    protected virtual bool SetValue(string value) {
[2694]80      double val;
81      if (double.TryParse(value, out val)) {
82        Value = val;
[2665]83        return true;
84      } else {
85        return false;
86      }
87    }
[3054]88
89    #region IStringConvertibleValue Members
90    bool IStringConvertibleValue.Validate(string value, out string errorMessage) {
91      return Validate(value, out errorMessage);
92    }
93    string IStringConvertibleValue.GetValue() {
94      return GetValue();
95    }
96    bool IStringConvertibleValue.SetValue(string value) {
97      return SetValue(value);
98    }
[2676]99    #endregion
[2]100  }
101}
Note: See TracBrowser for help on using the repository browser.