Float to string (Clean style)

Maya often returns interesting number when trying the put a float/double into a string.
Example float: 5.963111949e-19

I wanted to limit the number of decimals of a float/double number stored in a string. I needed on of my functions to generate(print) a call for another function. The resulting code should be user editable/readable.
Example (script generated code): kctScriptedShapeEdit(“joint3″,”$legCtrls[3]”,”doubleCircle”,”6.2″,”black”,”x”,2,{0,0.249,-0.328,0,0,0,1,1,1});

To do this I had to clean up the float, before turning it into a string. Here is a small funtion to that will do the work for you:
global proc string kctFloatToString(float $inputFloat, int $decimals)
int $dec = pow(10,$decimals);
string $stringFloat = ((float)((int)($inputFloat*$dec))/$dec);
if ($decimals != 0)
if (!`gmatch $stringFloat “*.*”`)
$stringFloat = ($stringFloat+”.0″);

return $stringFloat;

Leave a Reply

Your email address will not be published.