• 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
Question by maximal1542 · Jul 03, 2016 at 12:20 PM · c#errorscorenumbersrounding

Hello everyone I have a problem with rounding number of my score

{English is not my first language}
I made a clicker game and I did script with " Gold per second ". So when I my score is changing it become like that : 1.00002 or 4.000078. and I want to be like that : 1 or 4. Please help !! if you need more details ask me.

Comment

People who like this

0 Show 1
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 gjf · Jul 02, 2016 at 11:09 PM 0
Share

if you want to displayed integers then cast your values to an int- without seeing your code it's difficult to be more explicit.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by jgodfrey · Jul 03, 2016 at 04:10 PM

In a simple division, if both values are INTs, the result will be an INT (so, just a whole number). If one or both values are FLOAT, the result will be FLOAT.

It'd be easier to answer t$$anonymous$$s with some actual code to comment on, but...

I assume you are doing somet$$anonymous$$ng like t$$anonymous$$s:

 int gold = 10;
 float time = 2.4f;
 float goldPerSec = gold / time;
 Debug.Log(goldPerSec); // Prints 4.166667

In the above, since the "time" variable is a float, the result of the division (10 / 2.4) will also be a float (4.166667). If you want just the INT portion of that value, simply cast the result to an INT, and be sure to store the result in an INT. So...

 int gold = 10;
 float time = 2.4f;
 int goldPerSec = (int)(gold / time);
 Console.WriteLine(goldPerSec); // Prints 4
Comment

People who like this

0 Show 0 · 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

Answer by El-Deiablo · Jul 04, 2016 at 02:47 AM

If your trying to round a string you can use:

float.ToString("F2"); - T$$anonymous$$s rounds 2 decimal places - F1 would be one decimal place...etc

There's also mathf.round - https://docs.unity3d.com/ScriptReference/Mathf.Round.html

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

error CS0103 rb doesn't exist in the current context, ligne 8 and 15 , can't fix it 1 Answer

Error: The type `bool' cannot be used as type parameter `TEventArgs' in the generic type or method `System.EventHandler'. There is no boxing conversion from `bool' to `System.EventArgs' 0 Answers

How to reset score (GUI Label) to 0 when the killzone is touched and the level is reloaded? 1 Answer

Error when adding an item to a list 0 Answers

How can I fix UnityEngine.Random.Range (Int32 min, Int32 max) Stack Overflow 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