Click here to Skip to main content
Rate this: bad
good
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 22:54pm
lukeer15K
Rate this: bad
good
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:
"1.0.024"
 

Regards,
 
Manfred
  Permalink  
v4
Comments
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
   
Thanks!
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
good
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
  Permalink  
Comments
VJ Reddy at 14-Jun-12 5:35am
   
Good suggestion. 5!
SoMad at 14-Jun-12 5:38am
   
Thanks!
Manfred R. Bihy at 14-Jun-12 7:31am
   
That's the way to do it, do it right! :)
5+
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)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2014
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