Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by DarKAeRiX · Aug 26, 2016 at 04:45 AM · tostringchar

How to generate a string of zeroes based on a pre-determined algorithm?

I have the following code in place, and would like to condense the content of 'Update' down to a few lines at maximum. The code determines what an on-screen score counter displays, depending on the string length of the score.

 using UnityEngine;
 
 public class ScoreCounter : MonoBehaviour
 {
     public int score;
     public GameObject GameManager;
 
     void Update ()
     {
         score = GameManager.GetComponent<GameLoop>().stats.score;
         
 
         if(score.ToString().Length == 1)
         {
             this.GetComponent<TextMesh>().text = "Score: 0000" + score + "000";
         }
         else if(score.ToString().Length == 2)
         {
             this.GetComponent<TextMesh>().text = "Score: 000" + score + "000";
         }
         else if (score.ToString().Length == 3)
         {
             this.GetComponent<TextMesh>().text = "Score: 00" + score + "000";
         }
         else if (score.ToString().Length == 4)
         {
             this.GetComponent<TextMesh>().text = "Score: 0" + score + "000";
         }
         else if (score.ToString().Length == 5)
         {
             this.GetComponent<TextMesh>().text = "Score: " + score + "000";
         }
 
 
         
     }    
 }

I've calculated the number of zeroes needed in the first 'Score' string, which is:

 5 - score.ToString().Length

I'm fairly sure that the next step here is to implement it as follows:

 this.GetComponent<TextMesh>().text = "Score: " + 'insert code here with algorithm calculating zero count' + score + "000";

So the question is, what goes in that space, and if I'm completely wrong, what's the solution?

Thanks in advance for any help.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by JedBeryll · Aug 26, 2016 at 05:14 AM

this.GetComponent().text = score.ToString("D5"); will always make it look like 5 digits.

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 Bunny83 · Aug 26, 2016 at 06:29 AM 2
Share

And to get the desired output you would add the 3 zeros at the end:

 score.ToString("D5") + "000";

Or what would also work (but is less efficient) is to multiply the number by 1000 before you turn it into a number with 8 digits:

 (score*1000).ToString("D8");

The first one is better since it keeps the full range of the integer value. However since you only show 8 digits it would be still enough since an integer can go up to 2147483647.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Comparing words in dictionaries 1 Answer

Infinite loop crash System.Enum.ToString() 0 Answers

int to char conversion in JS ? 2 Answers

Float parse issue FormatException: Unknown char: - 0 Answers

This line of code works in the editor, but not on my android device 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