2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.sensorcommunity.internal.utils;
15 import org.eclipse.jdt.annotation.NonNullByDefault;
18 * The {@link NumberUtils} class provides helpers for converting Numbers.
20 * @author Bernd Weymann - Initial contribution
23 public class NumberUtils {
24 public static final double UNDEF = Double.NaN;
26 public static double round(Object o, int places) {
27 double value = convert(o);
29 // for negative places return plain number
34 long factor = (long) Math.pow(10, places);
35 value = value * factor;
36 long tmp = Math.round(value);
37 return (double) tmp / factor;
40 public static double convert(Object o) {
41 // ensure value not null
43 if (o instanceof Number) {
44 value = ((Number) o).doubleValue();
45 } else if (o instanceof String) {
46 value = Double.parseDouble(o.toString());