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 Mike Ledwards · Jan 13, 2011 at 12:08 PM · mathround

How can I round a number?

My fallDamage gets returned as something like 10.2367844 depending on distance.

How can I round this up or down in script?

I could just say if var > 1 but < 2 = 1, but I'd have to do that right the way through to 100.
Is there an easier way?

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

Answer by Statement · Jan 13, 2011 at 12:14 PM

Use Mathf.Round, Mathf.Ceil, Mathf.Floor, or cast them to ints:

int asInt = (int)floatValue;

For your example, you want to use Mathf.Floor or int casting, depending on your needs. Mathf.Floor return a float value. Casting returns of course an int value. Both approaches will get rid of any decimals.

Comment
Add comment · Show 2 · 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 Mike Ledwards · Jan 13, 2011 at 12:26 PM 0
Share

well i didn't think it would be that simple :D thanx

avatar image Statement · Jan 13, 2011 at 01:27 PM 0
Share

Be also sure to check out Borgo's reference to $$anonymous$$athf.FloorToInt.

avatar image
1

Answer by Borgo · Jan 13, 2011 at 12:19 PM

You can use .Net functions in your scripts, take a look at Round function documentation:
http://msdn.microsoft.com/en-us/library/system.math.round(v=vs.71).aspx

Take a look at Mathf functions too:
http://unity3d.com/support/documentation/ScriptReference/Mathf.html

http://unity3d.com/support/documentation/ScriptReference/Mathf.FloorToInt.html

Example:
// Prints 10
Debug.Log(Mathf.FloorToInt(10.7));

Comment
Add comment · Show 2 · 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 Statement · Jan 13, 2011 at 12:21 PM 1
Share

Yes, but these does not operate on float, which is a common type in game program$$anonymous$$g. You'd have to cast to decimal or double for every operation and it's a quite time consu$$anonymous$$g task.

avatar image Statement · Jan 13, 2011 at 12:22 PM 0
Share

... and then back again to float, or int.

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

"bleeding" when using configurable joints 2 Answers

Math Round? 2 Answers

Is it possible to round down 0.99 to 0? 2 Answers

Math.round not work 1 Answer

Rounding to multiples of 0.5 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