Reputation: 1520
I have string
variable that contain price. For example, 10000
. I want use a space for thousand separator.
e.g. display 10000
as 10 000
, 150000
as 150 000
How can I do this?
Upvotes: 1
Views: 470
Reputation: 3246
Convert manually using linq:
var n = "10000000";
var s = n
.Select((c, i) => c + ((n.Length - i - 1) % 3 == 0 ? " " : ""))
.Aggregate((s2, s3) => s2 + s3);
Upvotes: 2
Reputation: 4652
Try this
NumberFormatInfo info = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
info.NumberGroupSeparator = " ";
Console.WriteLine(12345.ToString("n", info )); // 12 345.00
Upvotes: 6