var versionNo = new System.Version("2.01");
I am getting the value versionNo
= 2.1
, but I want it as 2.01
.
Any suggestions, please?
That's how system.Version
works - it stores the components of the version as separate integers, so there's no distinction between 2.01
and 2.1
. If you need to display it that way you could format it as:
Version versionNo = new Version("2.01");
string s = string.Format("{0}.{1:00}",versionNo.Major, versionNo.Minor);
For convenience you could also create an extension method:
public static string MyFormat(this Version ver)
{
return string.Format("{0}.{1:00}",ver.Major, ver.Minor);
}
That way you can still customize the display while retaining the comparability of the Version
class.
Each component of System.Version is an int, so it doesn't pad the 0. You could have a major, minor, and build:
Version versionNo = new Version("2.0.1");
which results in
Version = 2.0.1
but generally, 01 and 1 are equivalent. If you want to specifically have the any component of the Version 0 padded, I suggest you override the ToString method of version or create an extension method (as mentioned by D Stanley), or use a different type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With