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
0
Question by TheRichardGamer · Oct 29, 2013 at 05:47 PM · function updatetransform.var typewriting to text

How to remove decimals from Vector3.Distance

How do I remove the decimals from my script so that my text mesh doesn't show something like 20.62634325 meters?

 var Other : Transform;
 
 function Start () {
 
 }
 
 function Update () {
 
   transform.localRotation = Other.localRotation;
 
 
 }
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

3 Replies

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

Answer by Loius · Oct 29, 2013 at 05:51 PM

Well there's absolutely no reference to anything in that code you posted, so not sure how that's supposed to help.

There are a couple easy ways to drop the extra digits:

 displayValue:int = Mathf.RoundToInt(floatValue);

or

 displayValue:int = (int)floatValue;
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 TheRichardGamer · Oct 29, 2013 at 07:14 PM 0
Share

This is what I mean by 'decimals' : alt text

decimals.png (251.6 kB)
avatar image
3

Answer by fafase · Oct 29, 2013 at 07:22 PM

ToString() can be overloaded:

 float fl = 0.1234567f;
 print(fl.ToString("0.00")); // prints 0.12

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 Huacanacha · Oct 29, 2013 at 07:32 PM 1
Share

This. Ignore the other answers.

You can format also Vector3's the same way which can be very useful for debugging, especially as the default is only 1 decimal place.

 >  (Vector3.up*0.04f).ToString();
 (0.0, 0.0, 0.0)
 >  (Vector3.up*0.04f).ToString("0.00");
 (0.00, 0.04, 0.00)
avatar image TheRichardGamer · Oct 30, 2013 at 07:06 AM 0
Share

Thank you Louis, but there's this thing with the distance, it's too much, like this: alt text

14meters.png (291.3 kB)
avatar image Loius · Oct 31, 2013 at 10:03 PM 0
Share

Please don't ignore the fact that you can convert floats to integers :P

avatar image Huacanacha · Oct 31, 2013 at 10:09 PM 0
Share

No don't ignore that fact entirely... just ignore it when you want a float as a String for display at a certain precision :)

avatar image
1

Answer by Kiloblargh · Oct 29, 2013 at 05:51 PM

Multiply by 100, convert to an int, then convert back to a float and divide by 100, that will give you just 2 decimal places.

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

17 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

Related Questions

Cooldown button system 1 Answer

Help on attachment script! 0 Answers

Stop a enemy by pointing a light at it. 4 Answers

How to stop spawn after a limit (int) 1 Answer

Why am I getting an error? Transform issue? 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