Converting number item numericState to integer

I am currently migrating my rules from DSL to JS script which is not a problem in general but now I started to fine tune code.
I simply want to increase the value of a number item by 1.
In DSL it was fairly simple

var i = vCounter.state as Number + 1

The result was an integer value as the item holds an integer value (5).

However in JS:

var i = items.vCounter.numericState + 1;

results in a decimal value (5.0)

What do I need to change? Parseint seems to me not the right approach as it parses string values. Also I do not like to use split() or something similiar. Math.floor or ceil are rounding functions which might do the job but wanted to know if there is an appropriate function for this.

numericState state always returns a floating point number. When you do math with a floating point number, you end up with a floating point number. To convert a floating point number to an integer you need to, as you guessed, round it up or down.

Rounding is the appropriate function.

However, do you need this to be an integer? It wasn’t really an integer in Rules DSL either based on the line of code you show. Rules DSL just didn’t show the decimal place to indicate it was when logging it.

Hi Rich,
thanks for your answer.

No, I could also do the conversion in my widget or use stateDescription.

I just regard it as “cleaner” if I solve the problem at the source i.e. in my rule where the value is created instead of correcting issues at the front end level.

As said, it is an integer counter I’d like to increase in my rule and it will be shown on one of my widgets. If I do an item.postUpdate(5), then the widget shows 5.0
In this case round function will do the job as a workaround for a missing toInt function.

Anyway, by investigating furhter I stumbled across the ~~ operator which also does th job of mitting decimal digits

Keep in mind that, to put it a certain way, computers are bad at math. Sometimes they only get really close the the correct answer because there are simply some numbers that cannot be represented. If you round values too soon, the errors very rapidly build up to the point where the results of calculations can become very far from correct.

The rule of thumb is to never round a number until just before showing it to a human.

If that’s the case, you can’t fix this in your rule. You’ll have to fix it in the widget or, more properly, the State Description. That’s really where display formatting configs should go anyway.