#,### problem

  • Thread starter Thread starter Jason Huang
  • Start date Start date
J

Jason Huang

Hi,

In my C# Windows Form, I would like to input a number "1234" and the output
is "1,234".
What do I need to do?
Thanks for help.


Jason
 
you can use Insert(int, string) on the string object.

string s = "1234";
s.Insert(1,",");

this solution will only work for 4 num char so to make it dynamic you will
have to use intelligent insert. You can use RegEx for that (e.g. for digits
like 100,000, or 1,200,000.34)
 
A better solution would be to convert the string to a number, using the
Parse method on the Int32 structure, and then to call the ToString method,
passing n or N for the format (which allows you to use group sizes).

Hope this helps.
 
Thanks!
Would you go into more details? Doesn't the C# already has an existing
class for this?

Nicholas Paldino said:
A better solution would be to convert the string to a number, using the
Parse method on the Int32 structure, and then to call the ToString method,
passing n or N for the format (which allows you to use group sizes).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Pohihihi said:
you can use Insert(int, string) on the string object.

string s = "1234";
s.Insert(1,",");

this solution will only work for 4 num char so to make it dynamic you
will have to use intelligent insert. You can use RegEx for that (e.g. for
digits like 100,000, or 1,200,000.34)
 
Hi Jason,

That was what Nicholas said. If you have a string represenation of a
number "1234", then you need to parse it to an int (Int32)

int n = Int32.Parse("1234");

However, once you have the number, simply call ToString() on the number,
specifying the format as N or N0 (zero, indicating the number trailing
zeroes, default is 2)

MessageBox.Show( n.ToString( "N" ) );
 
Back
Top