- Home /
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"
Answer by roojerry · Sep 16, 2013 at 05:00 PM
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
What's meant by the comma is a decimal separator. Some cultures use "," while "." is used by others.
Answer by vexe · Sep 16, 2013 at 05:06 PM
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;
A brief explanation about extensions:
Extensions allow you to extend existing classes. Extend means adding methods to the class that you're extending.
Your extension class
mustbe static, a good conversion is to name the class with what you're extendingthe word Extensions, like
StringExtensions,FloatExtensions, etc.public static class FloatExtensionsYour extension methods also must be marked static.
public static string PrependWithNumIf you're confused about the
this float fparameter 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
PrependWithNumis actuallymyFloat
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 :)
No need to reinvent the wheel.... Unity uses .NET (or rather $$anonymous$$ono), make sure you know it.
Your answer
Follow this Question
Related Questions
3d text will not display float 1 Answer
Converting a .CSV string array to a float array 1 Answer
How to convert a float to a string and then have it appear in a gui rect 3 Answers
Unity3d fails when uses float value. 1 Answer
match float values 3 Answers