how to divide a string with number at a certain amount?
Hello, my game has a currency, and i am displaying that currency on a UI Text component. and when the amount get's to high, the numbers gets to hard to read.
example on what is happening: money: 1241435356 $
example of what i want to achieve: 1 241 435 356 $
i need spaces between "every third" number, to make them less harder to read.
Thanks!
Answer by jkramar · Jul 28, 2016 at 09:22 PM
You should be able to use C#'s standard numeric formatting.
See here for details.
Just ask if you need more explanation. I can provide some examples.
when i use "ToString("C3")" the number shows almost what i want to achieve, the only problem is that there is 3 zero's just before my number. it makes the whole number look bigger than it actually is :o
is there a way to remove decimals also while using the ToString("C3") ?
thanks
I'am just going to reply to my own comment, i fixed the problem i had, i typed ToString("C3") wich will give you 3 decimals after, you just have to put 0 at the number 3 ins$$anonymous$$d and POOF the decimals are gone!
Thanks you so much for helping me!
Answer by NoseKills · Jul 28, 2016 at 09:27 PM
In general the 'number format strings' with ToString() are the handiest way to format numbers to strings.
To get a space as the "group separator" you have to make your own NumberFormatInfo and specify your own group separator for it instead of the normal ',' as shown here
var nfi = (NumberFormatInfo)CultureInfo.InvariantCulture.NumberFormat.Clone();
nfi.NumberGroupSeparator = " ";
string formatted = 1234897.11m.ToString("#,0.00", nfi); // "1 234 897.11"
do you need to put in "using" of some sort to use the "NumberFormatInfo"?
Googling NumberFormatInfo will show you it's in namespace "System.Globalization"
All of C# is well documented so there's never a need to guess these types of things :)
Your answer
