Why Use Only Two Instructions When You Can Do The Same Task With Four?

I stumbled across the following line of code while reading through a MaxScript this morning:

size_label.text = (((siz * 100) as integer) / 100)) as string

That’s a multiply, cast, divide, and cast to get a float shown as a string reprensentation of an integer. The same operation can be done with just two casts as:

size_label.text = (siz as integer) as string

When you recast a float as an integer the decimal points get dropped automatically. Multiplying a float by a value to shift the decimal point, converting the result to an integer, and dividing by the same value will also give you a result with no decimal places. It’s slower and more obtuse, though.

Comments are closed.