# To String with 'n' zeros in front

Hi!

What I want to make instead of the string

1,5869 for example when I do (1,5869).ToString();

I want to get a string with zeros in front until it has a special length:

(1,5869f).ToString(); → n=4 → “0001,5869”

(30,72839f).ToString(); → n=7 → “0000030,72839”

I hope you understand what I mean…

So mainly what I want is any float as the following string:

“00000,0000”

“00000,0001”

[…]

“99999,9999”

I am also a little unsure what you mean with your comma. However `.ToString("000000000")` should format your integer to 9 digits, prefixing it with the correct number of 0s. Here’s the reference to other Custom Numeric Format Strings

This is a simple programming problem.

There’s tons of ways you could do it with. But to do it from your `float` number instance, you need to make a float extension. Watch this to understand how to make extensions.

``````public static class FloatExtensions
{
public static string PrependWithNum(this float f, int numToInsert, int nTimes)
{
string str = f.ToString();
for (int i = 0; i < nTimes - 1; i++) {
str = str.Insert(0, numToInsert.ToString());
}
return str;
}
}
``````

Now you could do stuff like:

``````float f = 123.456f;
string zeroes = f.PrependWithNum(0, 4); // the first parameter is what you want to prepend (in this case, 0)
string ones   = f.PrependWithNum(1, 6); // the second one is how many digits will be before the decimal point, after the prepending including what you prepended (in this case, 6)
print (zeroes); // 0123.456
print (ones);   // 111123.456;
``````

• Extensions allow you to extend
methods to the class that you’re
extending.

• Your extension class `must` be
static, a good conversion is to name
the class with what you’re extending

• the word Extensions, like `StringExtensions`,
`FloatExtensions`, etc.

``````public static class FloatExtensions
``````
• Your extension methods also must be
marked static.

``````public static string PrependWithNum
``````
• If you’re confused about the `this float f` parameter I passed, this is
the actual instance that called the
method, for ex:

``````float myFloat = 123.123f;
print (myFloat.PrependWithNum(5, 0); // 00123.123;
``````

The first parameter that gets passed
into `PrependWithNum` is actually
`myFloat`

If you didn’t like extensions, just write a normal static helper method and throw it somewhere:

``````public static string PrependWithNum(float f, int numToInsert, int nTimes)
{
string str = f.ToString();
for (int i = 0; i < nTimes - 1; i++) {
str = str.Insert(0, numToInsert.ToString());
}
return str;
}
``````

But then you’d have to call it like:

``````float f = 34.34f;
print (whereverYouPutYourMethodIn.PrependWithNum(f, 0, 3)); // 034.34
``````

Hope that was informative and helpful