-3

When fromatting integer number (e.g. 1234567890) I want groups of size 2 (hundreds), not of size 3 (thousands). I have tried something like this

int number = 1234567890;
string value = string.Format("{0:#,###0}", number); 

Desired value:

12,34,56,78,90

Actual value:

1,234,567,890                          
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 3
    Please post code of what you have tried so far – Ipsit Gaur Jul 20 '18 at 06:15
  • Nope, not a clue what you're actually asking about here. Are you trying to write a *function*? If so, what is the *signature* of that function, and *what have you tried*? Try to give us a clear *specification* of what you're trying to do, rather than just putting an "example" in front of us and expecting us to *deduce* the specification. – Damien_The_Unbeliever Jul 20 '18 at 06:17
  • int number = 1234567890 value = string.Format("{0:#,###0}", number); – Kaviyarasu N Jul 20 '18 at 06:22
  • _Research; experiment; debug; ship!_ [ask] –  Jul 20 '18 at 06:24
  • **[edit]** your question to show your code, don't try to put it in comments. – Damien_The_Unbeliever Jul 20 '18 at 06:26
  • Your question is not clear on many points, for instance are the input and outputs `string` or `int` or a combination? Always be specific and prefer C# over a description. – bommelding Jul 20 '18 at 06:33
  • @KaviyarasuN this problem has already been solved here: https://stackoverflow.com/a/4133475/4329813 – popsiporkkanaa Jul 20 '18 at 06:39
  • Look into `System.Globalization.NumberFormatInfo` – Hans Kesting Jul 20 '18 at 06:42
  • thank you all, and @popsiporkkanaa its working fine thanks.This is my first question here So, let me know if anything wrong in my question i will correct it in next time. – Kaviyarasu N Jul 20 '18 at 07:03
  • @KaviyarasuN You're welcome. As MickyD has mentioned, you should read the How to Ask (https://stackoverflow.com/help/how-to-ask) first. Your question may already has been answered before in a different way but the main point is that you should show your effort of trying to solve it. And in the end, please don't forget to mark "Accepted" to someone's answer or vote a comment as helpful – popsiporkkanaa Jul 20 '18 at 07:20
  • @DmitryBychenko - an awful lot of guesswork. Does the OP really have "an integer number" or was it a string? – bommelding Jul 20 '18 at 08:27

2 Answers2

0

Try this:-

            static void Main(string[] args)
            {           
            NumberFormatInfo nfi = new CultureInfo("en-US").NumberFormat;

            // Displays a value with the default separator (".").
            Int64 myInt = 123456789012345;
            Console.WriteLine(myInt.ToString("N", nfi));

            // Displays the same value with different groupings.
            int[] mySizes1 = { 2, 3, 4 };
            int[] mySizes2 = { 2, 2 };
            nfi.NumberGroupSizes = mySizes1;
            Console.WriteLine(myInt.ToString("N", nfi));
            nfi.NumberGroupSizes = mySizes2;
            Console.WriteLine(myInt.ToString("N", nfi));
            ReadLine();
            }

for further info plz refer this link :- https://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.numbergroupsizes(v=vs.110).aspx

0

There can be many logic for doing this. I found following one. I assume you have string.

static void Main(string[] args)
{
    string numbers = "1234567890";

    Console.WriteLine(string.Join(",", CustomSplit(numbers, 1)));
    Console.WriteLine(string.Join(",", CustomSplit(numbers, 2)));
    Console.WriteLine(string.Join(",", CustomSplit(numbers, 3)));

    Console.ReadLine();
}

public static List<string> CustomSplit(string Input, int Length)
{
    List<string> result = new List<string>();


    string[] split = new string[Input.Length / Length + (Input.Length % Length == 0 ? 0 : 1)];

    for (int i = 0; i < split.Length; i++)
    {
    result.Add( Input.Substring(i * Length, i * Length + Length > Input.Length ? 1 : Length));
    }

    return result;
}

OUTPUT

1,2,3,4,5,6,7,8,9,0
12,34,56,78,90
123,456,789,0
Gaurang Dave
  • 3,956
  • 2
  • 15
  • 34