12,402,189 members (71,767 online)
Rate this:
See more:
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);
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?
Posted 13-Jun-12 21:54pm
lukeer16.5K

Rate this:

## Solution 1

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"

Regards,

Manfred
v4
Manas Bhardwaj 14-Jun-12 4:26am

Correct +5
Manfred R. Bihy 14-Jun-12 4:30am

Thanks Manas!
VJ Reddy 14-Jun-12 4:31am

Very good answer. 5!
Manfred R. Bihy 14-Jun-12 4:32am

Thanks!
amitgajjar 14-Jun-12 4:38am

i got answer as "1002.4.000".
Manfred R. Bihy 14-Jun-12 7:34am

If you use the code exactly as I wrote it you'll get the same result as I did. Maybe you forgot to quote one of the periods?
amitgajjar 14-Jun-12 7:43am

i can see many revisions for this answers, it may happen i have checked previous code....
lukeer 14-Jun-12 7:33am

Thanks a lot. 5!
Manfred R. Bihy 14-Jun-12 7:34am

You're welcome!
Rate this:

## Solution 3

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.

VJ Reddy 14-Jun-12 5:35am

Good suggestion. 5!

Thanks!
Manfred R. Bihy 14-Jun-12 7:31am

That's the way to do it, do it right! :)
5+

Thanks Manfred!
lukeer 14-Jun-12 7:35am

In general, yes. But this time, I have no influence on data formats. Neither in nor out. Just have to get the formatting done with what is given.

Sorry to hear that, but I understand.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 285 Richard Deeming 198 Karthik Bangalore 180 F-ES Sitecore 135 Tomas Takac 100
 OriginalGriff 7,073 ppolymorphe 3,030 Karthik Bangalore 2,917 F-ES Sitecore 2,187 Richard MacCutchan 2,125

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 Jun 2012