Attachments

Hipfile

jamesr_ftrim.hiplc

Problem

Sometimes you want to reference the value of a parameter and display it as a string to put in a Font SOP, or the Viewport Comment of a Camera node when you’re wedging sims or making some sort of visualizer.

But quite often, if you’re referencing a float parameter, you wind up getting allllll the digits that come with it, full precision and all, rather than just the nice value you see in the interface.

Super long string of numbers when channel referencing

Super long string of numbers when channel referencing

ie.

1
0.04

becomes

1
0.040000000000000001

which is probably not what you want!

A possible solution to trim off some of the extra digits might look something like

1
floor(ch("/some/parm") * 1000)/1000

Unfortunately, this fails too :(

Solution

The solution is actually quite simple! We can use the ftrim() function from HScript. ftrim() will strip off all those unwanted digits and leave you with a nice clean value, pretty much as you typed it!

1
Some Parameter Value: `ftrim(ch("/some/parameter"))`
Using `ftrim()` to shore it up

Using ftrim() to shore it up

Of course, this also works in a Font SOP too.

Demo on a Font SOP

Demo on a Font SOP

Update 9 Jan 2022

Here’s a handy snippet for programmatically wrapping channel references in ftrim(). Taken from the Linewriter tool.