Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by NinjaSquirrel · Aug 05, 2011 at 04:02 PM · mathmathfround

Round to nearest .5?

How do I take a float: x, and round it to the nearest .5? So if x is 5.1324 it would round to 5. But if x is 5.4216 it would round to 5.5 etc.

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
16

Answer by Graham-Dunnett · Aug 05, 2011 at 04:12 PM

multiply it by 2, round, then divide by 2???

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

Answer by Meltdown · Aug 05, 2011 at 04:17 PM

 float number = 5.4216f;
 number = (float)Math.Round(number, MidpointRounding.AwayFromZero) / 2;
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 SisterKy · Aug 05, 2011 at 07:46 PM 0
Share

While Graham's answer is perfect in it's simplicity, this caught my attention... Would you $$anonymous$$d to explain what this does?

googeling this, I've come to believe the (float) is type-casting in c#?? can you do this in js with 'as float'? (or am I mixing things up here?)

Some more research told me, this is not misspelled $$anonymous$$athf.Round but it's System.$$anonymous$$ath.Round
But isn't $$anonymous$$ath.Round(number, $$anonymous$$idpointRounding.AwayFromZero) basically the same as $$anonymous$$athf.Round(number)?
Why are you dividing it by 2?? Is this like Grahams idea, just you forgot to multiply number by 2 before Rounding it?

((thanks for making me look into the .net documentation... so far I've kept shying away from it :p ))

Greetz, $$anonymous$$y.

avatar image Meltdown · Aug 05, 2011 at 07:58 PM 0
Share

Hi $$anonymous$$y, the difference between $$anonymous$$ath.Round and $$anonymous$$ath.Round with the $$anonymous$$idpointRounding.AwayFromZero enumeration is that when AwayFromZero is specified, and the number is halfway between two other numbers, it is rounded toward the nearest number that is away from zero, ins$$anonymous$$d of being rounded to a zero.

avatar image SisterKy · Aug 05, 2011 at 09:21 PM 0
Share

Uhm... not the difference between $$anonymous$$ath.Round and $$anonymous$$ath.Round with $$anonymous$$idpointRounding but $$anonymous$$athf.Round and $$anonymous$$ath.Round with $$anonymous$$indpointRounding...

Weird!! For some reason I don't get e-mail notification if you answer me?! Almost missed your comment about html-tags (you really didn't see it formatted? What browser are you using? this worries me; thought the tags were foolproof...) and if I hadn't checked back here wouldn't have known about it... :( Are you getting notifications about me?

Greetz, $$anonymous$$y.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Rounding to multiples of 0.5 1 Answer

Math.Abs slows down on time.timescale 0 Answers

How I can increase num 1 to num 2 in x time? 1 Answer

Rotate Center Pivot based Object like a Draw Bridge 2 Answers

Round to 0.5, 1.5, 2.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