- Home /
How to add more commands in the ToString();
I want to have a large number with no decimals and commas. i know i can do ToString("n2"); for commas and ToString("0f"); for no demials. how do i put both in?
Example ToString("n2",0f"); (doesnt work)
Have you tried ToString("n2") + ToString("0f"); ? Not quite sure what the expected outcome you are looking for
Answer by Bunny83 · Jun 03, 2017 at 08:09 AM
Those aren't "commands" but format descriptors. You probably just want to use "D". However "D" only works with integral types (byte, short, int, ...) but not with floating point numbers (float, double, ...). However if you have a floating point number you can cast it to "int" or "long" before you call ToString.
Though the format "F0" should work as well. F0 has no 1000th-commas and no decimal digits.
D only removes decimals and doesnt add comas, does the same. is there no way to combine both or something, also thanks for the strong reply
Uhm you said you want a
number with no decimals and commas
So no decimals and no commas. If you wanted to add commas you should have worded that sentence different.
If you just want to remove decimals but want group seperators you can simply use "N0". Or as i said in my answer you can simply cast your floating point number into a "long" for example. long doesn't have any decimal digits.
// C#
float someNumber = 123456.789f;
long num = (long)someNumber;
string s = num.ToString("N");
or simply
// C#
string s = ((long)someNumber).ToString("N");
Yes thankyou, "N0" works! sorry to waste ya time'
Answer by FM-Productions · Jun 03, 2017 at 12:54 PM
You could try using the string.Format function
float variable1 = 13f;
int variable2 = 14;
bool variable3 = false;
string formattedStr = string.Format("My float = {0}, my int = {1}, my bool = {2}", variable1, variable2, variable3);
For information about formatting floats etc. with specified decimal digits, look here:
https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx
for example string.Format("{0:F0}", myFloatVariable);
should do the trick for floats without decimal digits. I haven't testet it though.
ah yeah, but im just going easy and use "N0" but thanks for da reply