Show entire ASP.net number using ToString () format
I have a problem with ToString (). I want to show all my number from this:
150000
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
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
.
source to share