Jump to content

preserve 00 digits in float display


pasto

Recommended Posts

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.

 

Link to comment
Share on other sites

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 by f1480187
  • Like 2
  • Thanks 1
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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];
}
  • Like 2
Link to comment
Share on other sites

  • 2 weeks later...

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...