Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Vector3.toString depth

Discussion in 'Scripting' started by gamesurgeon, Mar 24, 2011.

  1. gamesurgeon

    gamesurgeon

    Joined:
    Oct 11, 2009
    Posts:
    427
    Hello,

    Is there a way to tell Unity to add more decimals to the Vector3.toString command?

    For example, I am doing:

    Code (csharp):
    1. value = object.transform.position.ToString();
    And saving value to a file, which turns up to be something like (-3.5, 12.5, -9.5). However, I need more than one decimal of precision.

    Thanks for the help!

    EDIT.

    Sorry -- I didn't get much sleep last night. This was so easy that I feel that I insulted my intelligence!

    For anyone else who has this problem:

    Code (csharp):
    1.  
    2. var x : float = transform.position.x;
    3. var y : float = transform.position.y;
    4. var z : float = transform.position.z;
    5. var formattedOutput = "(" + x + ", " + y + ", " + z + ")";
    6. print(formattedOutput);
    7.  
    You don't even need the var x, y, z, but whatever!

    Hope that helps other sleep-deprived people (or those who do not actually know how to do this.)
     
    Last edited: Mar 24, 2011
  2. MADmarine

    MADmarine

    Joined:
    Aug 31, 2010
    Posts:
    627
    Since you can't get access to the ToString function you'd have to format it yourself:

    string.Format("{0:FX},{0:FX},{0:FX}", object.transform.position.x, object.transform.position.y, object.transform.position.z);

    Where X is the amount of decimal precision e.g. {0:F2} = 0.00
     
  3. gamesurgeon

    gamesurgeon

    Joined:
    Oct 11, 2009
    Posts:
    427
    More elegant solution!

    Thanks!
     
  4. chudin26

    chudin26

    Joined:
    Apr 7, 2011
    Posts:
    1
    More elegant solution is:

    position.ToString("0.000") - the result will be like 5.123.

    Sorry for my English :)