Weird problem with String.Format

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a fairly irritating problem. I have a class with a range of public
properties, one of which being 'CustomerAccountNumber' which is a string
type. At one point in my application I need this customer account number in
the format 'CAXXXXX XXXXX'. Now, I've tried a number of formatting options
including approaches like :

String.Format(object.CustomerAccountNumber,"CA##### #####");
Convert.ToInt64(object.CustomerAccountNumber).ToString("CA##### #####");
String.Format(object.CustomerAccountNumber,"{0:CA##### #####}");

But no matter what I do, the leading zero's get truncated. Am I missing
something obvious here?

Many thanks for any help you can offer.
 
Hi,
Assuming that the customer account number can be converted to an int, give
the formatting string as in:
Convert.ToInt64(object.CustomerAccountNumber).ToString("CA00000 00000");

I have a fairly irritating problem. I have a class with a range of public
properties, one of which being 'CustomerAccountNumber' which is a string
type. At one point in my application I need this customer account number in
the format 'CAXXXXX XXXXX'. Now, I've tried a number of formatting options
including approaches like :

String.Format(object.CustomerAccountNumber,"CA##### #####");
Convert.ToInt64(object.CustomerAccountNumber).ToString("CA##### #####");
String.Format(object.CustomerAccountNumber,"{0:CA##### #####}");

But no matter what I do, the leading zero's get truncated. Am I missing
something obvious here?

Many thanks for any help you can offer.
 
Scott said:
I have a fairly irritating problem. I have a class with a range of public
properties, one of which being 'CustomerAccountNumber' which is a string
type. At one point in my application I need this customer account number in
the format 'CAXXXXX XXXXX'. Now, I've tried a number of formatting options
including approaches like :

String.Format(object.CustomerAccountNumber,"CA##### #####");
Convert.ToInt64(object.CustomerAccountNumber).ToString("CA##### #####");
String.Format(object.CustomerAccountNumber,"{0:CA##### #####}");

But no matter what I do, the leading zero's get truncated. Am I missing
something obvious here?

Many thanks for any help you can offer.

Use '0' (zero) in your format string instead of '#' to force
non-significant zeros to be printed:

Convert.ToInt64(object.CustomerAccountNumber).ToString("CA00000
00000");
 
Back
Top