Hey guys,
I’ve made a function to get the dimensions of my bounding box and i’m putting them into a sentence as a string by using str(varx)+srt(vary) etc.
I have even rounded the Integers by using Round() but when i turn them into strings, everything comes up with 1 decimal precision that’s always 0. (for example 125.0 x 450.0 x 1200.0)
how to get rid of that decimal?
Thanks!!
You’ll need to post some code. Casting an integer to a string using str(myInt)
does not add decimals.
Since you asked about splitting a string that’s how you do it
If you have any string, and you want to separate it on a specific char, you can use
splittedString=targetstring.Split(char)
So if you have a string 102.1 that you get from usertext, you can use Split which is rather faster than converting the string to a float, and then removing any decimal numbers.
splittedString=targetstring.Split(".")
splittedString[0] will be 102
That being said, since you’re starting off with a float, you can simply round the values, without splitting any strings
# Round your integers
var1=102.2
var2=103.4
var3=104.5
varx = round(var1)
vary = round(var2)
varz = round(var3)
# Convert the rounded integers to strings with no decimal places
var_string = "{} x {} x {}".format(int(varx), int(vary), int(varz))
# Print the resulting string you will notice It's rounded
print(var_string)
This is my code now.
def BoundingBox():
obj = rs.GetObject()
bb = rs.BoundingBox(obj, in_world_coords=True)
XL = bb[1].DistanceTo(bb[0])
YL = bb[3].DistanceTo(bb[0])
ZL = bb[4].DistanceTo(bb[0])
x = round(XL, 0)
y = round(YL, 0)
z = round(ZL, 0)
dims = str(x)+" x "+str(y)+" x "+str(z)+" h."
return dims
i then define a
dimensions = BoundingBox()
and in another defintion later, i print dimensions
Try changing this line:
dims = str(x)+" x "+str(y)+" x "+str(z)+" h."
To:
dims = "{} x {} x {} h.".format(int(x),int(y),int(z))
1 Like