Click here to Skip to main content
15,881,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying not to use the ',' char as a thousand separator when displaying a string, but to use a space instead. I guess I need to define a custom culture, but I don't seem to get it right. Any pointers?

eg: display 1000000 as 1 000 000 instead of 1,000,000

(String.Replace() is not the solution I'd like to use :P)

What I have tried:

decimal test = decimal.Parse(txt.Text);
txt.Text= test.ToString("0,0");
Here is what I tried...
Posted
Updated 28-Sep-16 5:49am

If you are being feed the number as a string with the thousands separator already in there, this is a functional but dirty solution.
C#
char[] numberText = value.ToCharArray();
for (int i = 0; i < numberText.Length; ++i)
{
 if (numberText[i].Equals(','))
 {
   numberText[i] = ' ';
 }
}

return new String(numberText);
 
Share this answer
 
Comments
TatsuSheva 27-Sep-16 6:35am    
the thousand seperator is not already there, and also I am using textbox
Foothill 27-Sep-16 9:17am    
You could just work through the characters adding spaces as you go. There are lots of ways to do this.

char[] numberText = Array.Reverse(value.ToCharArray()); // flip the characters so it will start with the LSD
StringBuilder sBuilder = new StringBuilder(); // use the StringBuilder class to build your number string one digit at a time
for (int i = 0; i < numberText.Length; ++i)
{
sBuilder.Append(numberText[i]); // append a digit
if (i % 3 = 0)
{
sBuilder.Append(' '); // if it's a thousands digit, add your space
}
}

// the result is backwards, so flip and return
return new String(Array.Reverse(sBuilder.ToString().ToCharArray()));
TatsuSheva 27-Sep-16 11:28am    
I found something interesting but I have a problem when I put some numbers in the textbox, the cursor comes always to the left ...
Here is the code:
doubletext=double.Parse(txt.Text);
CultureInfoculture=CultureInfo.CreateSpecificCulture("fr-FR");
txt.Text=text.ToString("N0",culture);
Do you have any idea ?
Foothill 27-Sep-16 11:43am    
Try setting the RightToLeft property of the textbox to txt.RightToLeft = RightToLeft.Yes
TatsuSheva 27-Sep-16 11:56am    
Didn't work
Google is your friend. I simply googled format numbers with spaces in c# and it returned 1.16 million results.

It appears the route that is suggested is to make your own custom number formatter. Although sometimes the easiest way out isn't half bad (.Replace(",", " "))

Google[^]

decimal - How would I separate thousands with space in C# - Stack Overflow[^]

formatting - Use a custom thousand separator in C# - Stack Overflow[^]
 
Share this answer
 
v2
Comments
TatsuSheva 26-Sep-16 13:50pm    
NumberFormatInfo nfi = (NumberFormatInfo)
CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
double text = double.Parse(txt.Text);
txt.Text= text.ToString("n", nfi);

I put this but I don't have what I need...
When I write 2500 I have 02 500 but I want 2 500
Ralf Meier 26-Sep-16 14:07pm    
I can't confirm that.
When I use the same code it builds "2 500.00" from the source-String "2500".
There must be something different at you ...
David_Wimbley 26-Sep-16 14:23pm    
For me as well, the following code works as expected

var number = "2500";
NumberFormatInfo nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
double text = double.Parse(number);            
var finalFormatNumber = text.ToString("n", nfi); 
Console.WriteLine(finalFormatNumber);


And outputs 2 500.00.

Like ralf mentioned, you must have something else going on in your app.
TatsuSheva 27-Sep-16 4:24am    
It is in a textbox when I write 2500 it becomes 02 500
Ralf Meier 28-Sep-16 11:06am    
And what result to you get if you use exactly the same code as shown by David Wimbley ?

I tried it too with a Textbox and it gets the same result (2 500.00).
So we should look where is the difference ...
Try this.
C#
txt.Text=String.Format("{0:C}",  test*(-1)).Replace(",", " ");

It will add currency sign also then replace the currency sign as below if you don't want
C#
txt.Text=String.Format("{0:C}",  test*(-1)).Replace(",", " ").Replace("$", "");
 
Share this answer
 
v4
Comments
[no name] 28-Sep-16 11:51am    
That's not what the OP asked.
IamWsk 28-Sep-16 11:57am    
I forgot to add something, See my solution now.I Added ".Replace(",", " ")"
[no name] 28-Sep-16 12:06pm    
You mean just use Replace like Solution 2 already said? How is your solution any different?
IamWsk 28-Sep-16 12:14pm    
My solution is much differ than Solution 2. You can see solution 2 has 9 lines of code and mine is only 1. With my solution I hope he will not get any problem.
[no name] 28-Sep-16 14:08pm    
Sorry Solution 1. It's the same.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900