- Home /
How can I count Points like in Super Mario? C#
I mean for example 000000000 and then adding 100 Points 000000100 like this. If I do it, it show it only 0 and than 100 not 000000100. How can I do it right?
Answer by bfowle · May 22, 2013 at 02:12 AM
C# ToString() formatting (much less code):
int val = 100;
...
val.ToString("D9"); // 000000100
Fantastic~ Exactly what I was looking for. Seems to work similar to what I am used to in php with sprint_f but even simpler~
Answer by 1dayitwillmake · May 21, 2013 at 11:16 PM
Something like this will work:
// value is the actual value
/// leadingZeros is the amount of padding you want -
formatNumber( value, leadingZeros ) {
var scoreString = "";
leadingZeros = Math.pow(10, leadingZeros); // 4 becomes 0000
while( leadingZeros >= 10 ) {
if( value < leadingZeros ) {
scoreString += "0";
}
leadingZeros /= 10; // 4000 becomes 400
}
scoreString += String(value);
return scoreString;
}
formatNumber(678, 5); // Outputs '000678'
Or going by robertbu's answer, something more "Unity like" such as:
int foo = 400;
string st = foo.ToString().PadLeft( 9 - foo.toString().length,'0'); // Add the difference in characters to the left
@1dayitwillmake - For String.PadLeft(), the first value is the total string width, so you don't have to subtract the string length.
Answer by hoy_smallfry · May 21, 2013 at 09:44 PM
Similar question:
http://answers.unity3d.com/questions/12981/seperate-large-numbers-with-comma.html
You need to format the number a certain way for it's string representation. By default, a number will only be shown with the most significant numbers. You need to specify how you want it formatted.
This page has a list of all the standard formats (US dollars, etc.), and this one shows what what you can use to create custom formatting. The first one on the custom page is probably the one you need:
int x = 100;
// converts to a string with at least 9 zeros.
string xStr = x.ToString("000000000");
Debug.Log(xStr);
Answer by robertbu · May 21, 2013 at 10:23 PM
You can use String.PadLeft() to pad the left side of the string with characters:
int foo = 400;
string st = foo.ToString().PadLeft(9,'0');
Debug.Log (">"+st+"<");
Your answer
Follow this Question
Related Questions
Score & High Score logic doesn't work 2 Answers
how to save a highscore 1 Answer
How to keep score? 1 Answer