• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by goo-muffin · Sep 16, 2013 at 04:26 PM · javascriptstringfloat

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"

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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

Comment
Bunny83
syclamoth
RenanRL

People who like this

3 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image goo-muffin · Sep 16, 2013 at 05:19 PM 0
Share

thx that was all :D

avatar image Eric5h5 · Sep 16, 2013 at 06:30 PM 1
Share

What's meant by the comma is a decimal separator. Some cultures use "," while "." is used by others.

avatar image

Answer by vexe · Sep 16, 2013 at 05:06 PM

T$$anonymous$$s 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 t$$anonymous$$s to understand how to make extensions.

 public static class FloatExtensions
 {
     public static string PrependWithNum(t$$anonymous$$s 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 t$$anonymous$$s 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 t$$anonymous$$s 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 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 t$$anonymous$$s float f parameter I passed, t$$anonymous$$s 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 :)

Comment
clunk47
TechSupportIncoming1
goo-muffin

People who like this

3 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Sep 16, 2013 at 06:29 PM 1
Share

No need to reinvent the wheel.... Unity uses .NET (or rather Mono), make sure you know it.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How do you check if a string has already been removed from a generic list? 1 Answer

Variable in String 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges