How to round off a number

I am using a slider that goes to 100%
But because I need a value of 255, I multiply it by 2.55 like this

val redValue= 2.55* (NEO_SLIDERRED.state as DecimalType)

but that gives me a value of 254.99999999999997

and ‘0’ ofcourse will become 0.0

How do I round those numbers and get rid of the decimal?
I will try (as Number) as soon as I restrated openhab as a consequence of the next error
//---------
Anyway, I do encounter another problem with this multiplication.
It starts generating an erreor and crashes openhan

java.lang.IllegalArgumentException: Value must be between 0 and 100
	at org.eclipse.smarthome.core.library.types.PercentType.validateValue(PercentType.java:50)
	at org.eclipse.smarthome.core.library.types.PercentType.<init>(PercentType.java:45)
	at org.eclipse.smarthome.core.library.types.DecimalType.as(DecimalType.java:153)
	at org.eclipse.smarthome.core.items.GenericItem.getStateAs(GenericItem.java:87)
	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.convertState(ItemUIRegistryImpl.java:485)
	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.getState(ItemUIRegistryImpl.java:464)
	at org.eclipse.smarthome.ui.basic.internal.render.AbstractWidgetRenderer.getState(AbstractWidgetRenderer.java:229)
	at org.eclipse.smarthome.ui.basic.internal.render.AbstractWidgetRenderer.preprocessSnippet(AbstractWidgetRenderer.java:92)
	at org.eclipse.smarthome.ui.basic.internal.render.SliderRenderer.renderWidget(SliderRenderer.java:46)
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.renderWidget(PageRenderer.java:164)
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:129)
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(PageRenderer.java:150)
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processPage(PageRenderer.java:92)
	at org.eclipse.smarthome.ui.basic.internal.servlet.WebAppServlet.service(WebAppServlet.java:152)
	at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(HttpServiceServletHandler.java:71)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
	at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:577)
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(HttpServiceContext.java:287)
	at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
	at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(JettyServerHandlerCollection.java:80)
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
	at org.eclipse.jetty.server.Server.handle(Server.java:499)
	at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
	at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
	at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
	at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
	at java.lang.Thread.run(Thread.java:745)

Obviously Openhab doesnt like me to multiply some thing to become bigger than 100

I’m sure there’s a better way to do it but this works:

val redValue = (2.55* (NEO_SLIDERRED.state as DecimalType) + 0.5).intValue()

(notice the + 0.5 so the value effectively ranges from 0 to 255)
BTW I tried it I don’t get any error when playing with the slider, the error must come from somewhere else

1 Like

Thanks, I will give that a try Dominique (as soon as I restart OpemHab)

Sadly though the error occurred twice now… after playing with the sliders. I will see what happens

Try:
val redValue= Math::round(2.55* (NEO_SLIDERRED.state as DecimalType).intValue)

3 Likes

Thank you. I will try it all :slight_smile:

Thanks Alpoy, Sominique. It seem sthat the ‘Math::round’ version worked for me.

Not all issues solved as that very tiny sidemap seems a bit unstable after I added the sliders., but at least one problem solved :slight_smile:

Sadly though the specific sitemap keeps crashing after playing with the sliders