String format doesn't work :-(

Hello,

I have very simple example:
vsTemp = “4.444444”
logInfo(“vsTemp”, “vsTemp: {}”, vsTemp)
vsTemp = vsTemp.format(“%.1f”, vsTemp)
logInfo(“vsTemp2”, “vsTemp2: {}”, vsTemp)

after format (“%.1f”, vsTemp) the value of vsTemp is still “4.444444” :frowning:

What I’m doing wrong?

Try this:

vsTemp = "4.444444"
loginfo(vsTemp)
vsTemp = (Math.round(vsTemp)).toFixed(1)
loginfo(vsTemp)

Should give you 4.0

or you can use this as well so it doesn’t round.

vsTemp = (Number(vsTemp)).toFixed(1)

I just did a google search to find the answer (javascript format decimal)

Could you try

vsTemp = 4.444444

unfortunately none of them work :frowning:

vsTemp = 4.444444 - (without quote marks) - Cant format from double to string. Before vsTemp is defined as String.

You can’t format the String “4.444444” as if it were a float number 4.444444. That’s what the f in %.1f means, format a floating point number.

var vsTemp = 4.444444
logInfo(“vsTemp”, “vsTemp: {}”, vsTemp)
var vsTempStr = String.format(“%.1f”, vsTemp)
logInfo(“vsTemp2”, “vsTemp2: {}”, vsTempStr)

Did you test them before publishing reply?
Your script result:
Script execution of rule with UID ‘05a281655e’ failed:

And very interesting thing - very, very similar second script is working, and this one not. It’s very confused in OH(!) And general lack of the documentation about scripting on OH site.

Yes of course.

2023-11-29 09:51:45.232 [INFO ] [org.openhab.core.model.script.vsTemp] - vsTemp: 4.444444              
2023-11-29 09:51:45.233 [INFO ] [rg.openhab.core.model.script.vsTemp2] - vsTemp2: 4.4   

Rules | openHAB // The concepts of Rules
Rules - Introduction | openHAB // How to create rules in MainUI, note there are six pages
Rules | openHAB // The syntax docs for Rules DSL, be sure to follow the links too

When using Rules DSL most of the stuff you are working with are Java so String (Java SE 17 & JDK 17) (for docs on String.format).

If you find you are struggling with Rules DSL, you have Blockly, two flavors of JavaScript (I recommend the later one), and jRuby which all have good reference docs and Blockly has built in help. Groovy is an option but the docs are not as good and Jython is an option but it should be considered deprecated.

Thank you for the links to docs. I excatly Ctrl-C/Ctrl-V on these 4 lines and I got:
2023-11-29 18:22:02.175 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘d7a8c70fe7’ failed: var vsTemp = 4.444444
logInfo(“vsTemp”, “vsTemp: null”, vsTemp)
var vsTempStr = String.format(“%.1f”, vsTemp)
logInfo(“vsTemp2”, “vsTemp2: null”, vsTempStr)

So maybe it depends on platform. I’m working on Windows 11 + Zulu Java. Maybe is this the problem?

Shouldn’t be. Maybe the post above has a backwards quote in it or something.

var vsTemp = 4.444444
logInfo("vsTemp", "vsTemp: {}", vsTemp)
var vsTempStr = String.format("%.1f", vsTemp)
logInfo("vsTemp2", "vsTemp2: {}", vsTempStr)

I originally copied and edited your original rule as posted instead of copying the whole rule from my script.

It helps to always use code blocks when posting to the forum. That preserves the quotes from formatting.

```
code goes here
```

You are MASTER! It works!

Tahnk you @rlkoshak! Have a nice evening!