• 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.writing to textvar type

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

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

Why does this for loop crash my unity? 1 Answer

Patrol script won't work. 1 Answer

reference an object without pre-assigning. 2 Answers

my object instantiates to much 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges