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
1
Question by Grimmy · Aug 22, 2010 at 10:54 AM · timeroperator

How do use the % operator?

Under a similar note to my other question at http://answers.unity3d.com/questions/18765/how-do-i-format-a-string-into-daysminuteshoursseconds I'd like to know how excatly I should use the % operator.

I need to calculate days,hours,minutes and seconds. So far I have..

displaySeconds= seconds % 60;
displayMinutes =seconds /60; 

..but I got this from an example and I've no idea what th % is doing or how to go from here to get days/hours.

Many thanks!

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

Answer by spinaljack · Aug 22, 2010 at 10:59 AM

displaySeconds = seconds % 60; 

This shows you the "remainder" of seconds / 60

e.g.

120 seconds / 60 would be equal to 2 with 0 remainder

or

121 seconds / 60 would be equal to 2.01666667 or 2 and 1 remainder

What that script does is tell you how many seconds you should display on a clock i.e. if the time was 2 seconds you'd display 2 seconds, if the time was 62 seconds you'd display 2 seconds as well.

The minutes part of the script is pretty straight forward, there are 60 seconds in a minute...

EDIT:

Similarly for days and hours, if you know how many seconds there are in an hour (3,600) and how many seconds in a day (86,400) You can calculate those using the same formula.

e.g.

displaySeconds = seconds % 60;
displayMinutes = (seconds / 60) % 60;
displayHours = (seconds / 3600) % 24;
displayDays = seconds / 86400;

I went and did an example JS:

var displaySeconds : int; var displayMinutes : int; var displayHours : int; var displayDays : int; var seconds : float;

function Update () { seconds = Time.time; displaySeconds = seconds % 60; displayMinutes = (seconds / 60) % 60; displayHours = (seconds / 3600) % 24; displayDays = seconds / 86400; }

function OnGUI(){ GUI.Label(Rect(0,0,200,30),"Time " + displayDays + " : " + displayHours + " : " + displayMinutes + " : " + displaySeconds); }

This will display the time since the program started in Days : Hours : Minutes : Seconds

Just swap out Time.time for whatever system you want to have shown in your game (game-time probably)

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 Grimmy · Aug 22, 2010 at 11:22 AM 0
Share

That's brilliant. $$anonymous$$any thanks!!

avatar image
0

Answer by Edy · Aug 22, 2010 at 11:01 AM

var seconds : int = 12345;

displaySeconds = seconds % 60; displayMinutes = seconds / 60;

It's very important the seconds variable to be int, otherwise the displayMinutes variable would receive a float value.

Comment
Add comment · Show 4 · 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 spinaljack · Aug 22, 2010 at 11:24 AM 0
Share

1/60 is still a decimal

avatar image spinaljack · Aug 22, 2010 at 11:37 AM 0
Share

you probably meant you should cast display$$anonymous$$inutes as an int

avatar image Edy · Aug 23, 2010 at 09:15 AM 0
Share

1/60 is cero (integer division) :P (i meant that the variable would receive a float value).

As display$$anonymous$$inutes will surely be used in a format string like {0:00} then it's better it to be an integer for avoiding the cast. Using the float value directly in the format string will round the display value to the nearest integer.

avatar image spinaljack · Aug 23, 2010 at 10:38 AM 0
Share

int casting the exponent part of the real number is simply omitted i.e. rounded down which is exactly what you want and either method has no fewer castings if you were using Time.time I guess your method is fine too.

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

No one has followed this question yet.

Related Questions

If value is multiple of 1 Answer

Is there a modulus operator in Unity JavaScript? 4 Answers

Timer will not reset properly. 1 Answer

Raycast and timer to select and lock-on to targets 1 Answer

For loop resetting itself, but needs to stop 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