Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
avatar image
0
Question by RealMTG · Dec 21, 2015 at 02:49 PM · floatdecimaltostring

Make a float display 10:07 instead of 10.7

Hi

I am working on a timer for my game and I want to add some extra details to it. Right now I am simply using currentLevelTime.ToString("F2); and this kinda works. But there are a few things I don't like about it. Firstly, when separating the whole number and decimals there's only a dot. I want it to be a colon instead. Secondly, when it shows, for example, 10.7. I would like it to always show two digits on the decimals. Like 10:07. The same with the whole number. Instead of showing 8.1 it should show 08:01. How can I accomplish something like this?

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

Answer by wibble82 · Dec 21, 2015 at 03:24 PM

Hey there

I'm guessing you want to split your time into minutes and seconds, then format it in a nice string. On that basis, here's some example code to:

  • Split a floating point number of seconds into minutes and remaining seconds

  • Make use of the .net formatting system to convert those to different strings

               float total_seconds = 193.3f;
     
                 //calculate the number of whole minutes, then subtract from total to get remaining seconds
                 float minutes = Mathf.Floor(total_seconds / 60.0f);
                 float seconds = total_seconds - minutes * 60.0f;
     
                 //(A) gives 3:13.30
                 //the minute is at least 1 integer digits (will show a 0 otherwise)
                 //the seconds will be at least 2 integer digits (padded with 0s), 
                 //and EXACTLY 2 fractional digits (padded with 0s)
                 Debug.Log(string.Format("{0:0}:{1:00.00}", minutes, seconds));
     
                 //(B) gives 03:13.30
                 //same as (A), but minutes must be at least 2 integer digits
                 Debug.Log(string.Format("{0:00}:{1:00.00}", minutes, seconds));
          
                 //gives 03:13
                 //same as (B), but with no fractional digits for the seconds
                 Debug.Log(string.Format("{0:00}:{1:00}", minutes, seconds)); 
     
                 //gives 3:13.3
                 //same as (A), but the '#' means we now do AT MOST 2 fractional digits
                 Debug.Log(string.Format("{0:0}:{1:00.##}", minutes, seconds));
    
    

The string formatting in .net is really handy once you get the hang of it. Key documentation that's useful is:

General string.Format documentation:

https://msdn.microsoft.com/en-us/library/system.string.format(v=vs.110).aspx

Custom numeric format documentation (can also be used in ToString functions):

https://msdn.microsoft.com/en-us/library/0c899ak8(v=vs.110).aspx

-Chris

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 RealMTG · Dec 21, 2015 at 05:09 PM 0
Share

Thank you! This worked great! =)

avatar image
0

Answer by KdRWaylander · Dec 21, 2015 at 03:18 PM

Hi,

You need to split your number in two Strings: one with "10" and the over one with "7".

Then count the length of the first part (let's call it string1), if the lenght is = 1 (or not 2), this means you have to add a 0 at the beginning.

 if (string1.Length < 2) 
    string1 = "0" + string1;

Same thing for the second string.

At the end of the day, you will want to display something like:

  timerText.text = string1 + ":" + string2;

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

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

Related Questions

Float without comma 1 Answer

ToString ("f0") is rounding my float 1 Answer

How to get type double to vector3? 0 Answers

Dont round decimal places? 2 Answers

float adds decimal points over time 2 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