Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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
6
Question by AntLewis · Sep 17, 2011 at 07:13 PM · vector3tostringrounding

Vector3.ToString

Hi I'm using the ToString method to convert a Vector3 to a string so I can save it to a text file. Unfortunately, when I do, it rounds the vector3 members and only save them to one decimal place. So Vector3 ( 0.454502f, 0.9543028f, 31.54063f ) becomes Vector3(0.5f, 0.1f, 31.5f)

Is there anyway to prevent this rounding?

Thanks!

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
31
Best Answer

Answer by Wolfram · Dec 19, 2011 at 12:33 PM

Yes, the default behaviour is very annoying, and makes handling small values such as normalized Vectors, or Colors useless.

However, you can call ToString() explicitly and apply a formatting parameters yourself:

  • For example, probably the most useful option (which I wished was default!) Vector3 ( 0.4f, 0.9543028f, 31.54063f ).ToString("G4") will show up to 4 significant digits for every component, and will show as:

    (0.4, 0.9543, 31.54)

  • The variant Vector3 ( 0.4f, 0.9543028f, 31.54063f ).ToString("F3") will always show 3 digits after the decimal point for every component, but again for very small numbers you'll lose precision. It will show as:

    (0.400, 0.954, 31.541)

This works both in JavaScript and C#.

Comment
Add comment · Show 6 · 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 dcheng334 · Jun 22, 2013 at 05:11 AM 0
Share

This was very helpful, thank you!

avatar image jahroy · Jun 22, 2013 at 06:06 AM 0
Share

Then click the little "thumbs up" button to indicate the answer was helpful. That helps others to find it.

avatar image dcheng334 · Jun 22, 2013 at 08:38 PM 1
Share

I left a comment since I do not have upvoting privileges! Unity's implementation of the stack exchange system is not very friendly to us newcomers...

avatar image jahroy · Jun 22, 2013 at 10:17 PM 0
Share

I see. That's unfortunate. I've sadly moved on from my Unity project, but still get emails sometimes. I read this answer and was shocked it had never been upvoted. Now I now why.

avatar image GRobecchi · Jul 05, 2017 at 10:05 AM 1
Share

I am surprised nobody suggested this, but the best option for @AntLewis is actually ToString("R")

R means "Round Trip" which is designed to ensure that the value later read from the string is the same as when it was serialized.

avatar image Wolfram GRobecchi · Jan 22, 2020 at 05:13 PM 0
Share

@GRobecchi : Interesting. However, note this suggestion in the .net docs: Note: Recommended for the BigInteger type only. For Double types, use "G17"; for Single types, use "G9".

avatar image
1

Answer by Tabemasu Games · Dec 17, 2014 at 01:58 PM

You can also write a Vector3 expansion, like this:

 using UnityEngine;
 
 public static class Vector3Extension {
     public static string Print(this Vector3 v) {
         return v.ToString("G4");
     }
 }

Then you have a new method available for your Vector3's:

 Vector3 v = new Vector3(0.0, 0.55421, 0.0);
 UnityEngine.Debug.Log(v.Print());
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 jahroy · Sep 17, 2011 at 07:31 PM

You could write your own function to do it correctly:

function writeVectorProperly ( theV : Vector3 ) 
{
    return "(" + theV.x + ", " + theV.y + ", " + theV.z + ")"; 
}
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

7 People are following this question.

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

Related Questions

Rounding a position against the surface of an object. 1 Answer

How precise is unity with it´s transforms? 2 Answers

Getting the same 'rounded' value of a Vector3? 3 Answers

How would I counteract the force of the velocity and set it to zero after the input has stopped? 3 Answers

How to keep the same position of the object instead of camera 0 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