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
1
Question by oliver-jones · Sep 08, 2011 at 02:40 PM · floatroundroundingoff

Rounding Off A Float?

Hello,

I have a float (timer) that is basically Time.fixedTime. I use the timer for a GUI label. I want to have it so that the timer only uses the format: 0.0, so ... two digits.

How would one do that, in JavaScript?

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 Sydan · Sep 08, 2011 at 02:45 PM

http://answers.unity3d.com/questions/50391/how-to-round-a-float-to-2-dp.html

This may contain your answer. More shortly:

 yourFloat = Mathf.Round(yourFloat * 10) / 10;
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 Sydan · Sep 08, 2011 at 02:46 PM 0
Share

although you will probably need to use 10.0 rather than 10.

avatar image AusAndrew19 · Mar 08, 2014 at 02:20 AM 0
Share

Thanks heaps for your answer, Thank god for grave digging :) Really helped thank you.

avatar image rileydabozo · Mar 28, 2014 at 11:50 PM 0
Share

Thanks! Exactly what I needed.

avatar image supa4plex2 · Apr 14, 2020 at 02:21 AM 0
Share

So simple... and yet so genius... You've saved me a lot of head scratching, thank you! And this also made me realise that I'm too dependant on methods, even on the simplest things. I'll work on changing that :p

But thanks again mate!

avatar image
0

Answer by Bunny83 · Apr 14, 2020 at 02:33 AM

Since the question was already bumped I'd like to add that the solution that @Sydan proposed has several issues. First of all rounding the the value itself could significantly change how it changes over time. if you manually add deltaTime the rounding would essentially remove the change since it's so insignificantly small.


Not updating the original variable when performing the rounding would avoid the first issue. However the second issue will still stand. That is if the value gets larger it might not be possible to round to a nice 1 digit decimal place due to floating point precision. So occationally the float value could have more than 1 decimal digit also if the fractional part is 0 the ".0" usually disappears.


All those issues can be avoided by just using

 string textToDisplay = yourFloat.ToString("F1");

This way you can keep your actual float value as it is. The standard format "F" just displays a floating point number. The "1" specifies the number of decimal places behind the decimal point. For more information see Standard Numeric Format Strings

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

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

9 People are following this question.

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

Related Questions

I dont want Decimals :P 1 Answer

Is there a setting to change integer rounding to 'Swedish Rounding'? 2 Answers

How to round or convert doubles? 2 Answers

Rounding to multiples of 0.5 1 Answer

How can I detect when a float value crosses an integer threshold? 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