Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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
4
Question by hellobard · Aug 17, 2013 at 08:04 PM · guiscorenumbers

Displaying score with zeros in front of var (i.e. 0025 instead of 25)

I am trying to display a score variable in my GUI with a certain amount of zeros in front of the variable number. So instead of showing my score as "25" I want to be displayed as "0025".

This sounds like a simple thing to do, but I'm not sure how to google an answer.

Here's my code:

 #pragma strict

 var LabelPoints : UILabel;
 
 function Awake(){
 Money = PlayerPrefs.GetInt("Money");
 Points = PlayerPrefs.GetInt("Points");
 
 }
 
 function Update (){
 // this tells the label to update the points score GUI label
 LabelPoints.text = ""+points.Points;
 
 }
 
 function GetMoney  (){
 // this adds +1 to the score in the points script
  points.Points+=1;
 
 }
 
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

4 Replies

· Add your reply
  • Sort: 
avatar image
12

Answer by Bunny83 · Aug 17, 2013 at 08:31 PM

Just use ToString like this:

     LabelPoints.text = ""+points.Points.ToString("0000");


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 hellobard · Aug 18, 2013 at 08:56 AM 0
Share

$$anonymous$$any thanks to all three for great alternative methods! Bunny83's method was the simplest to implement and works perfectly. Cheers for the quick help!

avatar image
2

Answer by Slobdell · Aug 17, 2013 at 08:38 PM

You can display it as a string instead and put the 0s in front. I'm going to show you in C# but it should be just about the same in javascript with minor differences you can figure out.

 // Change to how many digits you want
 int scoreLength = 4;
 
 // this is the score
 int score = 25
 
 // score as a string
 string scoreString = score.toString();
 
 // get number of 0s needed
 int numZeros = scoreLength - scoreString.length();
 
 string newScore;
 for(int i = 0; i < numZeros; i++){
      newScore += "0";
 }
 
 newScore += scoreSting;
 
 // your newScore will now be "0025"
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 Tokusunei · Apr 17, 2019 at 01:42 AM 0
Share

Thanks! XD I know this is back from 2013, but I got it working!

avatar image
1

Answer by RomanorumLegio · Aug 17, 2013 at 08:28 PM

In your string, you could add the amount of zeros you want, and have if statements to change the number of zeros.

For example:

 function Update (){
     if(points.Points <= 9){
         LabelPoints.text = "000" + points.Points;
     }
     if(points.Points <= 99){
         if(points.Points >= 10){
             LabelPoints.text = "00" + points.Points;
         }
     }
 }
Comment
Add comment · 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
1

Answer by tenryu · Jul 09, 2017 at 07:28 PM

The correct way to do this in C# is:

  int intValue = 1023983;
     
 // Display integer values by calling the ToString method.
 Console.WriteLine("{0,22}", intValue.ToString("D8");
    
 // Display the same integer values by using composite formatting.
 Console.WriteLine("{0,22:D8} {0,22:X8}", intValue);
   
 // The example displays the following output:
 //                     01023983

Source: https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-pad-a-number-with-leading-zeros

Comment
Add comment · 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

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

22 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 avatar image avatar image avatar image

Related Questions

Score display not working 1 Answer

How can i make a local high score system with C#? 3 Answers

Score/Points when player kills enemy (multiplayer) 1 Answer

Making a simple scoreboard 0 Answers

how can i flip my scoredisplay var guitext so that it appears upside down? 0 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