Tomek Pawlowicz wrote:Hi there.
Is there a way to round numbers to two digits after the coma?
Hi Tomek. You can use Lua's
string.format() function to truncate how many digits of floating point precision are shown in your Text+ node expression field output:
- Code: Select all
Text(string.format("%.02f", math.pi))
For a person running Fusion on a North American EN-US style localized operating system, the final result of this expression is the text:
- Code: Select all
3.14
If you are on a localized operating system that uses a comma for the decimal mark character instead of the period character, then output from string.format() should be handled and displayed correctly for your region when you run the same code snippet.
For this example string.format() based expression the key thing to experiment with is how the text "%.02f" works as a format specifier.
The variable is being treated as if it is a float number by writing in (%f) in the function.
The initial "%.02" part of the formatting string represents that 2 digits will be printed beyond the decimal mark giving an output like "0.00" or "0,00" depending in on your localized region. If you wanted four digits of precision beyond the decimal mark you would write in (%.04f) which would give you "0.0000" or "0,0000" for the output.
In this demo the variable for "math.pi" is merely used to write in a sample floating point number which represents the value of pi.
Here is a code snippet you can paste into the flow area of your Fusion composite to see the result:
- Code: Select all
{
Tools = ordered() {
Text1 = TextPlus {
CtrlWZoom = false,
Inputs = {
Width = Input { Value = 3840, },
Height = Input { Value = 2160, },
["Gamut.SLogVersion"] = Input { Value = FuID { "SLog2" }, },
Size = Input { Value = 0.3376, },
Font = Input { Value = "Open Sans", },
StyledText = Input {
Value = "3.14",
Expression = "Text(string.format(\"%.02f\", math.pi))",
},
Style = Input { Value = "Bold", },
ManualFontKerningPlacement = Input {
Value = StyledText {
Array = {
},
Value = ""
},
},
},
ViewInfo = OperatorInfo { Pos = { 1485, 280.5 } },
}
},
ActiveTool = "Text1"
}
The string.format function is very similar to c-code's printf() function in how it lets you control the way variables are formatted and printed. For more information on the string.format() function, it is described in the Lua documentation here:
https://www.lua.org/pil/20.htmlhttp://lua-users.org/wiki/StringsTutorial