pasto Posted February 11, 2016 Share Posted February 11, 2016 Hello, I am displaying a float number going from 0 to 5 in a few seconds and I would like Houdini to display it as 0.00 to 5.00, is there any hscript function that forces the zeros to be displayed ? I tried padzero but that's not its purpose of course. Many thanx. Quote Link to comment Share on other sites More sharing options...
f1480187 Posted February 11, 2016 Share Posted February 11, 2016 (edited) I don't know such expression, but it is easy task for Python. You always can evaluate Python from Expressions. pythonexprs("'%0.2f' % " + (hscript_expression)) pythonexprs("'%0.2f' % " + ch("../grid1/sizey")) You also may switch expression language and use Python directly: '%0.2f' % some_float '%0.2f' % ch("../grid1/sizey") '%0.2f' % (hou.time() / 10.0) And maybe 10-15 different solutions exists. Really depends on your actual scene. Edited February 11, 2016 by f1480187 2 1 Quote Link to comment Share on other sites More sharing options...
pasto Posted February 11, 2016 Author Share Posted February 11, 2016 Many thanx f1480187, but it doesn't seem to evaluate something else than float and simple $T or $F hScript expressions, something like a channel ch("../grid3/sizey") is ignored and returns an error. Sorry I don't know anything about python. I put this into the font sop text field : ` pythonexprs("'%0.2f' % ( ch("../grid3/sizey") ) ") ` thx a lot. Quote Link to comment Share on other sites More sharing options...
f1480187 Posted February 11, 2016 Share Posted February 11, 2016 (edited) My bad, fixed the post with more reliable syntax. Edited February 11, 2016 by f1480187 Quote Link to comment Share on other sites More sharing options...
pasto Posted February 12, 2016 Author Share Posted February 12, 2016 Thanx so much, works perfectly. Should I deduce that python has got many more options than Hscript or Vex when you need to deal with string, digits, configurations ? Quote Link to comment Share on other sites More sharing options...
f1480187 Posted February 13, 2016 Share Posted February 13, 2016 String formatting is indeed better in Python. VEX and HScript both have only couple of basic functions. Here another couple of solutions, for both Expressions and VEX. # Expression. ftoa(trunc($T/10)) + "." + substr(ftoa(frac($T/10)) + "000", 2, 2) # Multiline version. { float val = $T / 10; string whole = ftoa(trunc(val)); string fract = ftoa(frac(val)) + "000"; fract = substr(fract, 2, 2); return whole + "." + fract; } // In theory, it should work. But not in my current build. sprintf("%0.2f", some_float); // You still can do it. string whole = itoa((int) some_float); string fract = sprintf("%f", frac(some_float)); string result = whole + fract[1:4]; // Or even write a function. string format_num(float val; int fract_digits) { string strval = sprintf("%f", val); string parts[] = split(strval, "."); string whole = parts[0]; string fract = parts[1]; return whole + "." + fract[:fract_digits]; } 2 Quote Link to comment Share on other sites More sharing options...
pasto Posted February 27, 2016 Author Share Posted February 27, 2016 Many thanx asd, this is so useful when dealing with motion design. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.