Show entire number using ToString () format

I have a problem with ToString (). I want to show all my number from this:



For this:

150 000


Here's what I've tried:

string myNumber = Convert.ToInt64(input_value.Text).ToString("D");


string myNumber

is a variable that I will bind to the label to display the entered value ( input_value

). This value is equal to <asp:TextBox>

. Finally, I want to add a space every 3 digits to make the value clearer.

Is there any other solution for ASP.NET? Because this one doesn't seem to work.

Note. I also tried with [...].ToString("N1");

and it works fine, but I don't need the last one .0

in the displayed result 150 000.0

that is not a valid integer.


source to share

3 answers

You can create a custom one NumberFormatInfo

with " "

as thousands separator:

var formatInfoWithSpaceGroupSep = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();
formatInfoWithSpaceGroupSep.NumberGroupSeparator = " ";
formatInfoWithSpaceGroupSep.NumberDecimalDigits = 0; // or "N0" instead of "N" below
Console.Write(num.ToString("N", formatInfoWithSpaceGroupSep)); //123 234 000


You must use N

a format specifier
that supports group separators instead of D




Sorry for the poluate. I found my problem. Let me explain:

I didn't get this in formation N1

, 1

means "1 decimal place". So I just need to set the format string n0




You can use the # character to indicate the number of digits in your result set with a space separator:

 string num = Convert.ToInt64(input_value.Text).
                      ToString("# ### ### ### ### ### ###").
 //Output 150 000 and covers any Int64 digit in input_value.Text




All Articles