Click here to Skip to main content
15,886,003 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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

It will work if you insert the periods quoted:

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


The output string will be:
"1.0.024"



Regards,

Manfred
 
Share this answer
 
v4
Comments
Manas Bhardwaj 14-Jun-12 4:26am    
Correct +5
Manfred Rudolf Bihy 14-Jun-12 4:30am    
Thanks Manas!
VJ Reddy 14-Jun-12 4:31am    
Very good answer. 5!
Manfred Rudolf Bihy 14-Jun-12 4:32am    
Thanks!
AmitGajjar 14-Jun-12 4:38am    
i got answer as "1002.4.000".
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
 
Share this answer
 
Comments
VJ Reddy 14-Jun-12 5:35am    
Good suggestion. 5!
SoMad 14-Jun-12 5:38am    
Thanks!
Manfred Rudolf Bihy 14-Jun-12 7:31am    
That's the way to do it, do it right! :)
5+
SoMad 14-Jun-12 7:36am    
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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900