版本号 float、decimal 或 double

发布于 2024-10-02 06:54:22 字数 458 浏览 6 评论 0原文

我有一个文档管理系统,其中文档可以有多个版本。每个版本都会被保存,用户可以查看版本历史记录。

我想知道的是:版本号应该使用什么数据类型?小数、浮点还是双精度? 我使用的是 .NET 和 C#。

版本号从0.1开始,每个发布的主要版本将四舍五入到下一个整数。即 0.4 变为 1.01.3 变为 2.0 等。

当版本号达到 0.9< /strong> 并且添加了次要版本,当我添加到它时,我希望数字变为0.10而不是1.0。这是最大的问题。

任何建议表示赞赏。

谢谢。

I have a document management system where documents can have multiple versions. Each version is saved and users can view version history.

What I would like to know is: What datatype should I use for version numbers? Decimal, Float or Double? I'm using .NET and C#.

Version numbers start at 0.1 and each published major version will be rounded to the next whole number. i.e. 0.4 goes to 1.0 and 1.3 goes to 2.0 etc.

When a version numbers hits 0.9 and a minor version is added I would like the number to go to 0.10 not 1.0, when I add to it. This is the biggest issue.

Any suggestions are appreciated.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放肆 2024-10-09 06:54:23

System.Version

这已经存储了不同的部分,处理将其呈现为字符串(修订和构建组件仅在非零时用于显示,因此它们与您的情况无关)问题)并且(最重要的是)其他 .NET 开发人员已经理解了,并且不会导致混乱(如果我看到某些版本号的使用不是 System.Version 我我会花一些时间尝试找出为什么 Version 不足以胜任这项工作,以防万一这被证明很重要并且隐藏着令人讨厌的惊喜。如果它足以胜任这项工作,我'。我会对开发人员这样浪费我的时间感到恼火)。

您可以使用扩展方法轻松处理所需的递增方式:

public static Version IncrementMajor(this Version ver)
{
  return new Version(ver.Major + 1, 0);
}
public static Version IncrementMinor(this Version ver)
{
  return new Version(ver.Major, ver.Minor + 1);
}

System.Version

This already stores the different parts, deals with presenting it as a string (revision and build components are only used in display if they are non-zero, so their irrelevance to your case doesn't matter) and (best of all) is already understood by other .NET developers, and won't lead to confusion (if I saw some use of a version number that wasn't a System.Version I'd spend some time then trying to work out why Version wasn't good enough for the job, in case that proved important and hid a nasty surprise. If it was good enough for the job, I'd be irritated at the developer wasting my time like that).

You can deal with the means you want for incrementing easily with extension methods:

public static Version IncrementMajor(this Version ver)
{
  return new Version(ver.Major + 1, 0);
}
public static Version IncrementMinor(this Version ver)
{
  return new Version(ver.Major, ver.Minor + 1);
}
(り薆情海 2024-10-09 06:54:23

两个整数怎么样?一份用于重大修改,一份用于小修改?

How about two integers? One for major and one for minor revisions?

墨小墨 2024-10-09 06:54:23

为此创建您自己的数据类型

public struct VersionNumber : IComparable<ReleaseNumber>
{
  public int MajorVersion{get;set;}
  public int MinorVersion{get;set;}

  public VersionNumber( int major, int minor)
  {
    MajorVersion = major;
    MinorVersion = minor;
  }

  public override string ToString(){
    return major + '.' + minor;
  }

  public int CompareTo(VersionNumber other) {
    int result;
    result = MajorVersion.CompareTo(other.MajorVersion);
    if (result != 0) { return result; }
    return MinorVersion.CompareTo(other.MinorVersion);
  }
  public static bool operator <(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) < 0;
  }
  public static bool operator <=(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) <= 0;
  }
  public static bool operator >(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) > 0;
  }
  public static bool operator >=(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) >= 0;
  }
}

您还可以添加一个比较器,以便您可以检查两个版本号,例如查看哪个版本号是两个版本号中的最高版本。

编辑

添加比较器逻辑也是为了很好的措施:)

Make your own data type for this

public struct VersionNumber : IComparable<ReleaseNumber>
{
  public int MajorVersion{get;set;}
  public int MinorVersion{get;set;}

  public VersionNumber( int major, int minor)
  {
    MajorVersion = major;
    MinorVersion = minor;
  }

  public override string ToString(){
    return major + '.' + minor;
  }

  public int CompareTo(VersionNumber other) {
    int result;
    result = MajorVersion.CompareTo(other.MajorVersion);
    if (result != 0) { return result; }
    return MinorVersion.CompareTo(other.MinorVersion);
  }
  public static bool operator <(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) < 0;
  }
  public static bool operator <=(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) <= 0;
  }
  public static bool operator >(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) > 0;
  }
  public static bool operator >=(VersionNumber left, VersionNumber right) {
    return left.CompareTo(right) >= 0;
  }
}

You can also add a comparer so you can check two version numbers to see which one is the highest version of two version numbers for example.

EDIT

Added the comparer logic also for good measure :)

生活了然无味 2024-10-09 06:54:23

我建议使用两个整数:一个大整数和一个小整数。如果您想要一个变量,您甚至可以将其存储为 major * 1000 +minor

I would suggest two integers: a major and a minor. You can even store this as major * 1000 + minor if you want one variable.

等你爱我 2024-10-09 06:54:23

小数应该是上面给出的最好的,但正如其他人指出的那样,两个整数会更好。

双精度和浮点数不能准确存储所有十进制值,您不希望您的版本突然变成 1.219999999999999999

Decimal should be the best of the above given, but as other has noted two ints would be better.

Doubles and floats does not accurately store all decimal values, you don't want your version to suddenly be 1.219999999999999999

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文