Package com.helger.commons.math
Class RoundHelper
- java.lang.Object
-
- com.helger.commons.math.RoundHelper
-
@Immutable public final class RoundHelper extends Object
Helper class for rounding numeric values in a flexible way.- Author:
- Philip Helger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classRoundHelper.EDecimalType
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static StringgetFormatted(double dValue, int nScale, RoundHelper.EDecimalType eType, Locale aLocale)Source: http://www.luschny.de/java/doubleformat.htmlstatic StringgetFormattedExp(double dValue, int nScale, Locale aLocale)static StringgetFormattedExp2(double dValue, Locale aLocale)static StringgetFormattedFix(double dValue, int nScale, Locale aLocale)static StringgetFormattedFix2(double dValue, Locale aLocale)static doublegetRounded(double dValue, int nScale, RoundingMode eMode, RoundHelper.EDecimalType eType)Source: http://www.luschny.de/java/doubleformat.htmlstatic doublegetRoundedEvenExp(double dValue, int nScale)Round using theRoundingMode.HALF_EVENmode and exponential representationstatic doublegetRoundedEvenExp2(double dValue)Round using theRoundingMode.HALF_EVENmode, exponential representation and a precision scale of 2.static doublegetRoundedUpFix(double dValue, int nScale)Round using theRoundingMode.HALF_UPmode and fix representationstatic doublegetRoundedUpFix2(double dValue)Round using theRoundingMode.HALF_UPmode, fix representation and a precision scale of 2.
-
-
-
Method Detail
-
getRounded
public static double getRounded(double dValue, @Nonnegative int nScale, @Nonnull RoundingMode eMode, @Nonnull RoundHelper.EDecimalType eType)Source: http://www.luschny.de/java/doubleformat.html- Parameters:
dValue- The value to be roundednScale- The precision of the decimal scale. If type isRoundHelper.EDecimalType.FIXthe decimal scale, else the (carrying scale - 1). Should be ≥ 0.eType- The formatting type. May not benull.eMode- The rounding mode to be used. May not benull.- Returns:
- the rounded value according to the specified rules. For NaN and infinite values, the input value is returned as is.
-
getRoundedUpFix
public static double getRoundedUpFix(double dValue, @Nonnegative int nScale)Round using theRoundingMode.HALF_UPmode and fix representation- Parameters:
dValue- The value to be roundednScale- The precision scale- Returns:
- the rounded value
-
getRoundedUpFix2
public static double getRoundedUpFix2(double dValue)
Round using theRoundingMode.HALF_UPmode, fix representation and a precision scale of 2.- Parameters:
dValue- The value to be rounded- Returns:
- the rounded value
-
getRoundedEvenExp
public static double getRoundedEvenExp(double dValue, @Nonnegative int nScale)Round using theRoundingMode.HALF_EVENmode and exponential representation- Parameters:
dValue- The value to be roundednScale- The precision scale- Returns:
- the rounded value
-
getRoundedEvenExp2
public static double getRoundedEvenExp2(double dValue)
Round using theRoundingMode.HALF_EVENmode, exponential representation and a precision scale of 2.- Parameters:
dValue- The value to be rounded- Returns:
- the rounded value
-
getFormatted
@Nonnull public static String getFormatted(double dValue, @Nonnegative int nScale, @Nonnull RoundHelper.EDecimalType eType, @Nonnull Locale aLocale)
Source: http://www.luschny.de/java/doubleformat.html- Parameters:
dValue- the value to be formattednScale- The precision of the decimal scale. If type isRoundHelper.EDecimalType.FIXthe decimal scale, else the (carrying scale - 1). Should be ≥ 0.eType- The formatting type. May not benull.aLocale- The locale to be used for the decimal symbols. May not benull.- Returns:
- the string representation of the double value. For NaN and infinite
values, the return of
Double.toString()is returned.
-
getFormattedFix
@Nonnull public static String getFormattedFix(double dValue, @Nonnegative int nScale, Locale aLocale)
-
getFormattedExp
@Nonnull public static String getFormattedExp(double dValue, @Nonnegative int nScale, Locale aLocale)
-
-