r/programminghorror • u/Jernesstar • May 18 '26
C# formatting big numbers
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace SandboxProject
{
static class Comma
{
// Function to put commas in long numbers to make them readable
public static string Num(long y){
string ss = ""; // Creates empty string ss
string s = Convert.ToString(y); // Converts number to string
switch (s.Length)
{ // Checks length of string and sets commas accordingly
default:
return s;
case 4:
ss += $"{s.Substring(0,1)},{s.Substring(1)}";
return ss;
case 5:
ss += $"{s.Substring(0,2)},{s.Substring(2)}";
return ss;
case 6:
ss += $"{s.Substring(0,3)},{s.Substring(3)}";
return ss;
case 7:
ss += $"{s.Substring(0,1)},{s.Substring(1,3)},{s.Substring(4)}";
return ss;
case 8:
ss += $"{s.Substring(0,2)},{s.Substring(2,3)},{s.Substring(5)}";
return ss;
case 9:
ss += $"{s.Substring(0,3)},{s.Substring(3,3)},{s.Substring(6)}";
return ss;
case 10:
ss += $"{s.Substring(0,1)},{s.Substring(1,3)},{s.Substring(4,3)},{s.Substring(7)}";
return ss;
case 11:
ss += $"{s.Substring(0,2)},{s.Substring(2,3)},{s.Substring(5,3)},{s.Substring(8)}";
return ss;
case 12:
ss += $"{s.Substring(0,3)},{s.Substring(3,3)},{s.Substring(6,3)},{s.Substring(9,3)}";
return ss;
case 13:
ss += $"{s.Substring(0,1)},{s.Substring(1,3)},{s.Substring(4,3)},{s.Substring(7,3)},{s.Substring(10)}";
return ss;
case 14:
ss += $"{s.Substring(0,2)},{s.Substring(2,3)},{s.Substring(5,3)},{s.Substring(8,3)},{s.Substring(11)}";
return ss;
case 15:
ss += $"{s.Substring(0,3)},{s.Substring(3,3)},{s.Substring(6,3)},{s.Substring(9,3)},{s.Substring(12)}";
return ss;
case 16:
ss += $"{s.Substring(0,1)},{s.Substring(1,3)},{s.Substring(4,3)},{s.Substring(7,3)},{s.Substring(10,3)},{s.Substring(13)}";
return ss;
case 17:
ss += $"{s.Substring(0,2)},{s.Substring(2,3)},{s.Substring(5,3)},{s.Substring(8,3)},{s.Substring(11,3)},{s.Substring(14)}";
return ss;
case 18:
ss += $"{s.Substring(0,3)},{s.Substring(3,3)},{s.Substring(6,3)},{s.Substring(9,3)},{s.Substring(12,3)},{s.Substring(15)}";
return ss;
case 19:
ss += $"{s.Substring(0,1)},{s.Substring(1,3)},{s.Substring(4,3)},{s.Substring(7,3)},{s.Substring(10,3)},{s.Substring(13,3)},{s.Substring(16)}";
return ss;
case 20:
ss += $"{s.Substring(0,2)},{s.Substring(2,3)},{s.Substring(5,3)},{s.Substring(8,3)},{s.Substring(11,3)},{s.Substring(14,3)},{s.Substring(17)}";
return ss;
case 21:
ss += $"{s.Substring(0,3)},{s.Substring(3,3)},{s.Substring(6,3)},{s.Substring(9,3)},{s.Substring(12,3)},{s.Substring(15,3)},{s.Substring(18)}";
return ss;
}
}
}
}
38
Upvotes
29
u/Jernesstar May 18 '26
4 years ago, was still new to programming. Part of a Collatz Conjecture calculator I was making.
Didn't know there was a string function that handled this. Not clever enough to come up with an iterative solution.
5
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 18 '26
Is there a problem with just going backwards from the end of the string and adding a comma every three digits? Sure, you'll need to make sure the new buffer can accommodate the extra commas, and either you or the library will need to take care of shifting everything over one memory space. First should be a matter of adding 1/3rd of the length rounding up.
46
u/New-Resolution9735 May 18 '26
Looks like production ready code to me
12
u/6502zx81 May 18 '26
Finally a leftpad() in C# I can rely on!
10
u/Aphrontic_Alchemist May 18 '26 edited May 20 '26
My take on an iterative solution.
Very simple compared to Knuth's -yllion system.
5
u/abigail3141 [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” May 18 '26
Hello switc case my old friend
5
u/zergea May 18 '26
string.Format(): look what they need to mimic a fraction of our power.
2
u/Quill09 May 21 '26
My take on a refactoring:
1
u/querela May 20 '26
Noob. Should have used recursion. Can even be optimized if tail-end recursion.
And if you want to generalize it a bit more, you could dynamically create and run code to do it.
1
u/bzzzt_beep May 22 '26
isn't it
or am i missing something