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
5
Question by sona.viswam · Sep 12, 2013 at 12:12 PM · floatround

round float with 2 decimal

 float val =  num1/num2;


The result of val is 2.7456

I need result as 2.75

How can i get it

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

5 Replies

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

Answer by DeveshPandey · Sep 12, 2013 at 12:22 PM

You can use Math class of C# to do this-

System.Math.Round(val,2);// Edited

This is a right way!!

Comment
Add comment · Show 11 · 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 sona.viswam · Sep 12, 2013 at 12:26 PM 0
Share

i need at least one decimal

avatar image neogamesproduct · Sep 12, 2013 at 12:27 PM 0
Share

mathf.roundtoint is that you need : http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.RoundToInt.html

avatar image sona.viswam · Sep 12, 2013 at 12:28 PM 0
Share

I don't want want int value. Please read my question.

avatar image sona.viswam · Sep 12, 2013 at 12:46 PM 3
Share

@Hoeleo: behave yourself

avatar image Hoeloe · Sep 12, 2013 at 12:55 PM 2
Share

Seriously. If you don't understand the basics of typing and casting, you will not be able to finish a game project. End of. This is fundamental stuff, and it will hit you later on, and hard. Learn the basics - I'm not just saying that because I don't want to help you, the opposite. I'm saying it because unless you know the basics, you will fail.

Show more comments
avatar image
10

Answer by Hoeloe · Sep 12, 2013 at 12:18 PM

Expanding on the other answer, you should use that method, which is Math.Round(val, 2);. The val here refers to the value you're rounding, while the 2 refers to the number of decimal places to round to.

Old answer for archive purposes

You can do quite a simple trick actually: Multiply it by 100, round it, divide it by 100. So, you multiply it by 100, giving 274.56. Then you round it, giving 275. Then divide it by 100 again, giving 2.75.

Alternatively, you can use C#s string.Format, like so: string.Format("0.0", val), which will display only 2 decimal places as a string value. You can then parse the string back into a float.

Of these two methods, I recommend the first one.

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 Hoeloe · Sep 12, 2013 at 12:25 PM 0
Share

Show your code please. That doesn't sound right at all.

avatar image Hoeloe · Sep 12, 2013 at 12:32 PM 0
Share

Yeah, there's no rounding there. Try what I suggested, or use the method suggested by DeveshPandey, using System.$$anonymous$$ath.Round(val,2); (Thanks for that, DeveshPandy, wasn't aware of those overloaded versions of $$anonymous$$ath.Round)

avatar image
3

Answer by whoopdeedoo03 · Aug 08, 2017 at 08:46 AM

Try using this if you would like to round to 2 decimal places:

val.ToString("F2");

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 EdgeKing810 · Aug 03, 2018 at 10:29 AM

Hey... I've got an idea.

Why don't you try assigning the following to a variable.

val2 = val.ToString("F2"); //This will convert your value to 2 decimal points.

Then, you could do the following to get it back as a float.

val3 = float.Parse(val2);

I'm writing this for future Unity users who may come across this. Oh and sorry if I've misspelled something in the code.

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
0

Answer by dahal2871 · Oct 27, 2020 at 11:14 AM

float val = num1/num2;
val = (float)System.Math.Round(val, 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

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

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

Related Questions

A node in a childnode? 1 Answer

C# round transform.position floats to .5 2 Answers

Shown decimal places 2 Answers

2D rigidbody makes my character slowly rise 1 Answer

Is it possible to round down 0.99 to 0? 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