Click here to Skip to main content
15,919,749 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new to this site and c# (a VB programmer ) so please excuse any of my ignorances.

My question is this I have a string representation of a number.

The number can be between 0 and 999999999999

I need to represent the the string always in the format of 000.000.000.000

For example:

12567 would be 000.000.012.567
1379468 would be 000.001.379.468
4500078912 Would be 004.500.078.912

Etc.

I have tried many methods including using regex, string format, etc. Nothing seems to work.

Any help would be greatly appreciated.

Thank you.

What I have tried:

Private String OriginalString;

OriginalString = "132456";

String.Format(############,OriginalString);

.....
Posted
Updated 19-Jan-18 23:17pm

For starters, don't use strings to represent numbers: it's a PITA when you want to do something with them later. Always holds data in the appropriate datatype, and convert to that type as early as possible using the Parse, TryParse, and / or TryParseExact methods (reporting problems to the user as you go) and convert back to a string for presentation as late as possible.
The "early in" and "late out" design lets you use information about the users Locale and Culture which may not be available while processing or if you store the info.

Then it's just a matter of formatting the number for output:
long x = 4500078912;
Console.WriteLine("{0:00#\\.###\\.###\\.###}", x);
string output = String.Format("{0:00#\\.###\\.###\\.###}", x);
 
Share this answer
 
Comments
Member 13631520 20-Jan-18 5:26am    
Hello, thanks for you response.

The value is typed as a string from one of the properties in a referenced class. The number is actually a get only value representing a frequency. However; you are correct, I should implement it in my code as a numeric value because I will have to use it later mathematically to calculate offsets. Strange the DLL author didn't expose it as numeric property value. Thanks for the tip. Saved me some time.
OriginalGriff 20-Jan-18 5:27am    
You're welcome!
There is a solution using regular expressions:
C#
string formatted = Regex.Replace(originalString, ".{3}", "$0.");
 
Share this answer
 

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