Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
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
Add comment
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
3
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
Add comment · 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
3

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 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 :)

Comment
Add comment · 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 $$anonymous$$ono), 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.

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

Unity3d fails when uses float value. 1 Answer

match float values 3 Answers


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