I can written an application which converts strings (made of numbers) from 8 – 12 characters in length (see examples below)
- 1404336133
- 4174728823
- 0587035281
Basically I want to convert the strings above into a specific format (stored in a config file) for the time being its as shown below.
<add key="Consumer_Code_Format_For_Code_Length_Eight" value="#### ####"/>
<add key="Consumer_Code_Format_For_Code_Length_Nine" value="### ### ###"/>
<add key="Consumer_Code_Format_For_Code_Length_Ten" value="### #### ###"/>
<add key="Consumer_Code_Format_For_Code_Length_Eleven" value="#### ### ###"/>
<add key="Consumer_Code_Format_For_Code_Length_Twelve" value="#### #### ####"/>
I am currently using the following code to format the codes …
public static string FormatCode(string code)
{
switch (code.Length.ToString())
{
case "8":
string codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eight"];
code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
break;
case "9":
codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Nine"];
code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
break;
case "10":
codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Ten"];
code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
break;
case "11":
codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eleven"];
code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
break;
case "12":
codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Twelve"];
code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
break;
default:
codeFormat = ConfigurationManager.AppSettings["Consumer_Code_Format_For_Code_Length_Eight"];
code = String.Format("{0:" + codeFormat + "}", Double.Parse(code));
break;
}
// Finally return the newly formatted code
return code;
}
However, for the code 0587035281 it displays “58 7035 281” therefore removing the leading zero which I require.
Any ideas how to stop this and also is there anything wrong or suspicious with my code?
Looking forward to your reply
1 Answer