将整数转换为版本控制字符串 [英] convert integer to versioning string

查看:71
本文介绍了将整数转换为版本控制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,专家,

我有一个整数.设为int a = 10024.它代表一个实际上会读取"1.0.024"的版本号.

我想使用

Hi experts,

I have an integer number. Let it be int a = 10024. It represents a version number which actually would read "1.0.024".

I would like to convert the integer to string (insert dots at fixed positions) using

string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0:000.00}",10024);

,但这还没有提供所需的输出(取而代之的是"10:072.00").

有没有办法调整string.Format()还是我必须使用10024.ToString()和几个Insert()?

but that''s not yet giving the desired output ("10:072.00" instead).

Is there a way to tweak string.Format() or will I have to use 10024.ToString() and several Insert()s?

推荐答案

如果插入句点,它将起作用引用:

It will work if you insert the periods quoted:

string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0:0'.'0'.'000}",10024);



输出字符串将是:



The output string will be:

"1.0.024"




问候,

Manfred




Regards,

Manfred


您可能可以按照解决方案1所述进行字符串格式化,但是根据我的经验,您的基本方法肯定会在以后引起重大问题.
假设您到了整数值为111024的地步.应该将其解释为11.1.024或1.11.024吗?您可能现在还没有考虑其他令人讨厌的事情-相信我,去过那里,做到了.

我建议您删除整数,而使用类 System.Version [ ^ ].看看它,并注意所有可以设置值的方式.
我怀疑内置的ToString()是否会创建所需的格式,但是您可以使用Major,Minor等属性自由地设置自己的字符串格式.

Soren Madsen
You will probably be able to do the string formatting as outlined in Solution 1, but in my experience, your basic approach is bound to cause major problems later on.
Say you get to the point where your integer has the value 111024. Is that supposed to be interpreted as 11.1.024 or 1.11.024? There are other nasty things that you probably are not considering right now - believe me, been there, done that.

I suggest you drop the integer and instead use the class System.Version[^]. Take a look at it and notice all the ways you can set the values.
I doubt the built-in ToString() will create the format you want, but then you are free to do you own string formatting, using the properties Major, Minor, etc.

Soren Madsen


这篇关于将整数转换为版本控制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆