Click here to Skip to main content
11,709,220 members (53,086 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# string Forms
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
Rate this: bad
Please Sign up or sign in to vote.

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:


Manas Bhardwaj at 14-Jun-12 4:26am
Correct +5
Manfred R. Bihy at 14-Jun-12 4:30am
Thanks Manas!
VJ Reddy at 14-Jun-12 4:31am
Very good answer. 5!
Manfred R. Bihy at 14-Jun-12 4:32am
amitgajjar at 14-Jun-12 4:38am
i got answer as "1002.4.000".
Manfred R. Bihy at 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 at 14-Jun-12 7:43am
i can see many revisions for this answers, it may happen i have checked previous code....
lukeer at 14-Jun-12 7:33am
Thanks a lot. 5!
Manfred R. Bihy at 14-Jun-12 7:34am
You're welcome!
Rate this: bad
Please Sign up or sign in to vote.

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.

Soren Madsen
VJ Reddy at 14-Jun-12 5:35am
Good suggestion. 5!
SoMad at 14-Jun-12 5:38am
Manfred R. Bihy at 14-Jun-12 7:31am
That's the way to do it, do it right! :)
SoMad at 14-Jun-12 7:36am
Thanks Manfred!
lukeer at 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.
SoMad at 14-Jun-12 7:43am
Sorry to hear that, but I understand.

Soren Madsen

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 580
1 OriginalGriff 250
2 Maciej Los 225
3 Peter Leow 219
4 Mika Wendelius 160
0 OriginalGriff 9,428
1 Sergey Alexandrovich Kryukov 9,027
2 CPallini 5,189
3 Maciej Los 4,991
4 Mika Wendelius 3,856

Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100