Class Rounder<T extends Number>

java.lang.Object
me.croabeast.common.Rounder<T>
Type Parameters:
T - the type of the number, which must extend Number

public final class Rounder<T extends Number> extends Object
A utility class for rounding numbers to a specified number of decimal places.

The Rounder class supports various numeric types (e.g., Double, Integer, Float, etc.) and provides methods to round a given number to a default of 2 decimal places or a custom amount. Internally, it formats the number into a string using a customizable pattern and then parses it back to the original numeric type.

  • Method Details

    • round

      public static <T extends Number> T round(T t)
      Rounds the given number to the default of 2 decimal places.
      Type Parameters:
      T - the type of the number
      Parameters:
      t - the number to be rounded
      Returns:
      the rounded number with 2 decimal places
    • round

      public static <T extends Number> T round(int decimalAmount, T t)
      Rounds the given number to the specified number of decimal places.
      Type Parameters:
      T - the type of the number
      Parameters:
      decimalAmount - the desired number of decimal places
      t - the number to be rounded
      Returns:
      the rounded number with the specified decimal places