5

I'd like to format an integer in a way that it has leading zeros and thousand separators at the same time.

I know that someInt.ToString("D6"); will give me leading zeros, but apparently it doesn't allow NumberGroupSeparator. On the other hand someInt.ToString("N"); will give me separators but no leading zeros...

Is it possible to combine both to print 123456 as 00 123 456? I know I can get string created with N and then manually add zeros to string in a loop or something, but maybe there's better way?

EDIT: Number of padding zeros (total digits length of number) should be adjustable.

2
  • 1
    Why would it be "00123 456" rather than "00 123 456"? And is space really your thousands separator?
    – Jon Skeet
    Commented Apr 3, 2012 at 8:41
  • Thanks, my mistake, I corrected it. It will be space or apostrophe.
    – Episodex
    Commented Apr 3, 2012 at 8:43

4 Answers 4

2

If you want 00 123 456, just do:

123456.ToString("00 000 000")

If you need a fixed amount of zero's, all I can think of is this:

int NUM_LENGTH = 6;

//This is not very elegant
var NUM_STR = String.Join("", Enumerable.Range(0, NUM_LENGTH).Select((x, i) => (NUM_LENGTH - i) % 3 == 0 ? " 0" : "0"));

//But at least it works:
var example1 = 123456.ToString(NUM_STR); //Outputs  123 456
var example2 = 1234.ToString(NUM_STR); //Outputs 001 234
9
  • I edited my question. I need number length to be adjustable. The formatting is for meter display image.
    – Episodex
    Commented Apr 3, 2012 at 8:47
  • You mean you need as many zeros as the length of the number? Otherwise you can do 12.ToString("00 000 000") or 123456.ToString("0 000") for example.
    – BlueVoodoo
    Commented Apr 3, 2012 at 8:52
  • I mean that I'd like to be able to set this length from a variable. This is closed control with a parameter that sets length of number (actual length of number doesn't matter). Normally I did it with 12345.ToString("D" + NUM_LENGTH); where NUM_LENGTH was this variable. Now I need thousand separators too...
    – Episodex
    Commented Apr 3, 2012 at 8:56
  • See my update. I can't think any other way of doing it unfortunately.
    – BlueVoodoo
    Commented Apr 3, 2012 at 9:28
  • I get a feeling there must be a better way to achieve what you want.
    – BlueVoodoo
    Commented Apr 3, 2012 at 9:29
2

I think the following should work.

int value = 102145;
int num_length = 10; // it may change as you expected
string format = "000,000,000,000,000";
string tmp = value.ToString(format);
Console.Out.WriteLine(tmp.Substring(tmp.Length - num_length - tmp.Length/4 + 1 ));

Please let me know whether it works or not.

Corrected & working version:

int value = 102145;
int num_length = 12;
string format = "000,000,000,000,000,000";
string tmp = value.ToString(format);
int totalLength = format.Replace("000,", "000").Length;
int rem = (totalLength - num_length ) / 3;
Console.Out.WriteLine(tmp.Substring(totalLength - num_length + rem));
6
  • It prints 9 digits when I set num_length to 8, but otherwise it works. +1 from me but I'm waiting for more answers :).
    – Episodex
    Commented Apr 3, 2012 at 9:43
  • ohh, I see. The formulla is not correct. I am working on this. Thanks
    – sarwar026
    Commented Apr 3, 2012 at 9:54
  • Please see the updated code. It works for any length. Please mark it also as an answer if it's okay with your purpose. Thanks
    – sarwar026
    Commented Apr 3, 2012 at 10:31
  • Since the code is a bit lengthy, you can write a common extension method so that it works like a single line function, i hope.
    – sarwar026
    Commented Apr 3, 2012 at 10:32
  • Thanks for update! Indeed this code works well now. I accepted the answer that I actually used. Mostly because it returns format string, so it leaves me freedom to choose separator through NumberFormatInfo. I gave you +1 for this answer as it is correct too. As far as I know it's not possible to mark two answers as Answer on SO though.
    – Episodex
    Commented Apr 3, 2012 at 12:10
0

You're trying to use the Standard format strings to acheive what you're after, however you're going to need to look at the Custom Numeric Format Strings (MSDN). These should enable to set things up exactly as you require.

0

Something like this

 .toString("#,###");
1
  • You guys want to take another shot at writing code that compiles? ;)
    – musefan
    Commented Apr 3, 2012 at 8:43

Not the answer you're looking for? Browse other questions tagged or ask your own question.