Reputation: 760
I have a string "123456" and I want to make it "123.456". Is there a function in c# that converts the value to what I need?
Other examples: "1000000" -> "1.000.000"
Upvotes: 0
Views: 7478
Reputation: 7213
First parse it to numeric type (depending what would be the length of your string), for example lets use long
(and long.TryParse()
method):
string str = "1000000";
long num = 0;
long.TryParse(str, out num); //or long.TryParse(str, out long num); in c# 7
then use ToString()
to convert it back to string using specified format:
string nstr = num.ToString("N0");
Some article about numeric string formats: Standard Numeric Format Strings
Upvotes: 5
Reputation: 121
If you want to do it by just making a call to a function :
private string AddSeparator(string value)
{
string newvalue = string.Empty;
try
{
newvalue = Convert.ToInt32(value).ToString("N");
}
catch
{
//Do whatever you want if conversion fails
}
return newvalue;
}
Upvotes: 0