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
22
Question by masterarcher · Mar 03, 2011 at 03:57 PM · mathfdecimal

how to round a float to 2 DP

i have found functions that truncate the decimal places to create an integer.

but i have the time in seconds and wish to have a set amount of decimal places say 3.

thanks

edit: in C# please

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

7 Replies

· Add your reply
  • Sort: 
avatar image
113
Best Answer

Answer by Mike 3 · Mar 03, 2011 at 04:04 PM

If it's just for changing to string:

yourFloat.ToString("F2")

If you want the float itself truncated:

yourFloat = Mathf.Round(yourFloat * 100f) / 100f;
Comment
Add comment · Show 1 · 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
24
Best Answer

Answer by yoyo · Mar 03, 2011 at 04:05 PM

To get time in thousandths of a second, you could do this (C# syntax) ...

float seconds = Time.time;
int thousandths = (int)(seconds * 1000.0f);

The C# system library includes Math.Round, which lets you pass in the number of decimal places you want to preserve. However it works with double and decimal numbers, which may not be desirable. Unity's Mathf.Round does not let you specify decimal places (feature request??), but you can implement your own version like this:

public static float Round(float value, int digits)
{
    float mult = Mathf.pow(10.0f, (float)digits);
    return Mathf.Round(value * mult) / mult;
}

Note that it will be more efficient to just perform the computation inline with your code, knowing the appropriate power of 10, for example:

float rounded = Mathf.Round(seconds * 1000.0f) / 1000.0f;

or (if you don't care about rounding):

float rounded = (int)(seconds * 1000.0f) / 1000.0f;

Finally, if this is just for formatting and display, not calculation, then there are several alternatives shown in this question.

Comment
Add comment · Show 1 · 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 InevitableDev · Apr 09 at 06:12 PM 0
Share

It is Mathf.Pow not Mathf.pow, just in case you get an error.

avatar image
24

Answer by estefanowolf · Nov 15, 2014 at 09:24 AM

 //create a double
 //create a float
 float a;
 double b;
 b = System.Math.Round(a,2);
 // replace number 2 with number of decimals you wish to round upto.
Comment
Add comment · Show 1 · 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 Shkeek · Oct 12, 2015 at 11:54 PM 2
Share

By far the simplest solution, System.$$anonymous$$ath.Round(a,2);

Question, will this allow the file size to be smaller, or is it the same as "using System;" namespace? In other words, will this only extract the math.round method from system without importing all the other methods?

avatar image
12

Answer by charmandermon · Aug 07, 2013 at 05:07 AM

just add this at the top:

 using System;


It will give you access to math.round

Then do something cool like this:

 speedCapped = (float)Math.Round((double)lerpedSpeed,2);

Its a little over the top but cool.

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
5

Answer by masterarcher · Mar 03, 2011 at 04:06 PM

i foudn this example in the end on stackoverflow.:

decimal a = 1.994444M;

Math.Round(a, 2); //returns 1.99

decimal b = 1.995555M;

Math.Round(b, 2); //returns 2.00

i think this is a little neater than yours Mike, cheers for the reply tho.

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 masterarcher · Mar 03, 2011 at 04:14 PM 0
Share

bah math isnt there and its not an overloaded function of mathf

avatar image yoyo · Mar 03, 2011 at 04:19 PM 0
Share

see my updated answer for an implementation of Round.

avatar image Mike 3 · Mar 03, 2011 at 04:22 PM 0
Share

Just include System ins$$anonymous$$d

  • 1
  • 2
  • ›

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

12 People are following this question.

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

Related Questions

Unity Mathf Float Minus Integer Rounding Error 1 Answer

How to change how meany decimals are in a GUI sound slider 1 Answer

decimals not allowed to update as global varibles from Unity to C# script?,Why are decimals not allowed to apply as global variable to be changed in Unity? 3 Answers

Mathf.Cos error 1 Answer

Is there a way to have an object gradually move to one spot, move back, and then stop? 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