2
cube1.name = string.Format("Terrain_{0}_{1}", (int)Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x, (int)Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y); 

Tried to cast it for int but it didn't change much. The name of each cube is still for example: Terrain_6.5_5.5. But I want the name to be Terrain_6_5.

tilePositionInLocalSpace is vector3 type and both x are float type.

1
  • 6
    Add brackets: (int)(Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x). Commented Jun 7, 2017 at 13:41

5 Answers 5

16

You've just got a precedence problem. You've got:

(int) x + y 

which is equivalent to

((int) x) + y 

... which will then promote the result of the cast back to a float in order to perform floating point addition. Just make the cast apply to the whole result instead:

(int) (x + y) 

Where x and y are the rather long expressions in your original code, of course. For the sake of readability, I'd extract the two values to separate local variables, so you'd have:

int sumX = (int) (Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x); int sumY = (int) (Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y); cube1.name = string.Format("Terrain_{0}_{1}", sumX, sumY); 

Or better still:

var position = Terrain.activeTerrain.transform.position; int sumX = (int) (position.x + tilePositionInLocalSpace.x); int sumY = (int) (position.z + tilePositionInLocalSpace.y); cube1.name = string.Format("Terrain_{0}_{1}", sumX, sumY); 
Sign up to request clarification or add additional context in comments.

2 Comments

One step back. Why not let the formatting do it's work? Thats the whole trick of formatting.
@JeroenvanLangen: I'd say that's less obvious in terms of effect than the casting, personally.
2

you need to cast both as int since now you are adding an int with a float which of course results to float. Try:

cube1.name = string.Format("Terrain_{0}_{1}", (int)(Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x), (int)(Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y)) 

2 Comments

Casting both is a bad idea - that will lose more information than it needs to, compared with casting the whole result to int.
Correct. Bad explanation. I wanted to say to cast the addition
1

Just cast the result of the addition:

(int) (x + y); 

And then string.format ()...

Comments

1

I would just specify the number of digits on the formatting. It can be done by entering a format.

Like this:

// here | | cube1.name = string.Format("Terrain_{0:0}_{1:0}", Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x, Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y); 

Comments

-1

you need to explicitly convert it:

cube1.name = string.Format("Terrain_{0}_{1}", Convert.ToInt32(Terrain.activeTerrain.transform.position.x + tilePositionInLocalSpace.x), Convert.ToInt32(Terrain.activeTerrain.transform.position.z + tilePositionInLocalSpace.y)); 

1 Comment

This is simply false. Convert.ToInt32 is simply a less efficient way of converting a float to an int. The purpose of Convert.* is for converting a value where you do not know it's type, but know that it's IConvertible, and that's simply not the case here. Using the cast operator is converting it, as there is an explicit conversion operator from float to int.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.