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(
	at org.eclipse.smarthome.core.library.types.PercentType.<init>(
	at org.eclipse.smarthome.core.items.GenericItem.getStateAs(
	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.convertState(
	at org.eclipse.smarthome.ui.internal.items.ItemUIRegistryImpl.getState(
	at org.eclipse.smarthome.ui.basic.internal.render.AbstractWidgetRenderer.getState(
	at org.eclipse.smarthome.ui.basic.internal.render.AbstractWidgetRenderer.preprocessSnippet(
	at org.eclipse.smarthome.ui.basic.internal.render.SliderRenderer.renderWidget(
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.renderWidget(
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processChildren(
	at org.eclipse.smarthome.ui.basic.internal.render.PageRenderer.processPage(
	at org.eclipse.smarthome.ui.basic.internal.servlet.WebAppServlet.service(
	at org.eclipse.jetty.servlet.ServletHolder.handle(
	at org.eclipse.jetty.servlet.ServletHandler.doHandle(
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceServletHandler.doHandle(
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(
	at org.eclipse.jetty.server.session.SessionHandler.doHandle(
	at org.eclipse.jetty.server.handler.ContextHandler.doHandle(
	at org.ops4j.pax.web.service.jetty.internal.HttpServiceContext.doHandle(
	at org.eclipse.jetty.servlet.ServletHandler.doScope(
	at org.eclipse.jetty.server.session.SessionHandler.doScope(
	at org.eclipse.jetty.server.handler.ContextHandler.doScope(
	at org.eclipse.jetty.server.handler.ScopedHandler.handle(
	at org.ops4j.pax.web.service.jetty.internal.JettyServerHandlerCollection.handle(
	at org.eclipse.jetty.server.handler.HandlerWrapper.handle(
	at org.eclipse.jetty.server.Server.handle(
	at org.eclipse.jetty.server.HttpChannel.handle(
	at org.eclipse.jetty.server.HttpConnection.onFillable(
	at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(
	at org.eclipse.jetty.util.thread.QueuedThreadPool$

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

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


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