Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Formatting Long variable as String

I'm trying to take a variable (long), and convert it to a string, such that:

150 -> 150
1500 -> 1,500
1234567 -> 1,234,567

I know this shouldn't be difficult, but so far, I've only been able to find two different solutions, and they're not giving me the output I want:

This: String.Format("{0:n}", *long variable*.ToString())

gives me: 2000 -> 2000

and this: *long variable*.ToString("N" or "N0")

gives me: 2000 -> 2000.00

like image 809
Jordan Foreman Avatar asked Jun 19 '26 15:06

Jordan Foreman


2 Answers

Someone commented the correct syntax on an answer that was deleted, so for the sake of anyone reading this in the future, here is what works:

String.Format("{0:#,##0}", *long variable*)

like image 98
Jordan Foreman Avatar answered Jun 21 '26 04:06

Jordan Foreman


Either of these work fine

string.Format("{0:n0}", someNumber);

string.Format("{0:#,##0}", someNumber);


These can be used with ToString() as well, (e.g. someNumber.ToString("n0");)

like image 41
Marc Avatar answered Jun 21 '26 04:06

Marc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!