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
5
Question by Krshojestic · Sep 04, 2012 at 09:44 AM · integer

How to set fixed number of digits?

I was wondering if there was a clean and short way to make a certain integer always to show with three digits, even if it is lower than 100 (i.e. 003, 005, 012, 057, 099). I do not like doing it like this:

if (i < 100) { if (i < 10) { someString = "00" + i.ToString(); } else someString = "0" + i.ToString(); } else { someString = i.ToString(); }

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
13
Best Answer

Answer by Bork Awesome · Sep 04, 2012 at 10:30 AM

The 'Dn' parameter of ToString() specifies the number of leading 0's; so for your example, requiring 3 leading zero's, the code would be:

threeDigitString = i.ToString("D3");

See the reference here: http://msdn.microsoft.com/en-us/library/dd260048.aspx

Comment
Add comment · Show 3 · 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 CelticKnight · Aug 02, 2017 at 12:39 AM 0
Share

I am using Unity 5 and the "D3" formatter threw up an error.

avatar image JPhilipp · Feb 19, 2018 at 11:00 AM 0
Share

For reference, "F2" is what worked for me, whereas "D3" threw an error.

avatar image Hydra-Scott · Jul 07, 2020 at 10:28 PM 0
Share

I'm using Unity 2020.1.0b8.3654 I used "d2" with no problems.

   string seconds = ((int)round1Timer % 60).ToString("d2");

avatar image
4

Answer by TheGameLearner · Apr 13, 2018 at 04:12 AM

i.ToString("D3") May not always work in Unity, but you have custom options that can be filled in as

 int i = 23;
 print("3 digit num = " + i.ToString("000") );

The same can be used to ensure padding for a minimum number of digits however many they might be.

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 lasdoo5 · May 29, 2018 at 06:24 PM 0
Share

tnx mate..

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

14 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

Related Questions

Dealing with Texture2D.width as it is a float number 3 Answers

Float to int casting in C# script with modulo giving division by zero error 1 Answer

How to break up a number into seperate digits? 2 Answers

Really weird behaviour with boolean variables from custom class 2 Answers

Idle Game Currency Optimizing 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