Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
2
Question by Fitty_Spenc · Apr 20, 2012 at 02:30 AM · gamespeeddecimalrounddriving

How to round a number?

I have a driving game and am displaying the speed in the corner. This is my code

var mph = rigidbody.velocity.magnitude * 2.237;

mphDisplay.text = mph + " MPH";

It works fine except it displays to like 5 decimal points because I converted it to MPH. I need to round it up to .5 and I know I need to use Mathf.Round, I just don't know how, can anyone help?

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
2

Answer by syclamoth · Apr 20, 2012 at 02:36 AM

If you want to round a number to an integer, Mathf.Round is the thing for you. If you only want to limit the number of significant figures, you can do something like this:

 var mph : float = Mathf.Round(rigidbody.velocity.magnitude * 22.37) / 10;
 // notice the factor of ten in the bit that gets rounded- adding one decimal point

But that's pretty hacky. There's probably a better way in the Float library in .net, but you can look that up yourself if you run into any more trouble.

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 aldonaletto · Apr 20, 2012 at 03:00 AM

To display a single digit after the decimal point, you can use the "F1" format:

var mph = rigidbody.velocity.magnitude * 2.237;
mphDisplay.text = mph.ToString("F1") + " MPH"; // displays one digit after the dot
But if you want to display multiples of 0.5, @syclamoth's idea can do the job with a little change:

var mph: float = Mathf.Round(rigidbody.velocity.magnitude * 2.237 * 2)/2;
mphDisplay.text = mph.ToString("F1") + " MPH"; // displays one digit after the dot
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 Fitty_Spenc · Apr 20, 2012 at 03:10 AM 0
Share

Thanks thats super helpful

avatar image okaybj · Mar 07, 2021 at 08:29 AM 0
Share

How can I use ToString("F1") but hide the dot? for example if the float was 6.9 but i wanted it to display as 69. idk if u will respond because you are sort of a legend to me I see you on so many forums posts but none the less thank you for all the help you have given me in other posts!

avatar image okaybj · Mar 07, 2021 at 08:57 AM 0
Share

so i figured it out.. for anybody who maybe comes by this post looking for the same thing... its:
speedText.text = (runSpeed.ToString("f1")).Replace(".", "");

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

6 People are following this question.

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

Related Questions

Why does my game run much faster on Windows than Linux 1 Answer

I dont want Decimals :P 1 Answer

How do I make an object look at a point it is moving toward? 1 Answer

changing speed for 10 seconds and then return it back to its value 0 Answers

Shown decimal places 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