xmlgraphics-commons 2.1

org.apache.xmlgraphics.util
Class DoubleFormatUtil

java.lang.Object
  extended by org.apache.xmlgraphics.util.DoubleFormatUtil

public final class DoubleFormatUtil
extends Object

This class implements fast, thread-safe format of a double value with a given number of decimal digits.

The contract for the format methods is this one: if the source is greater than or equal to 1 (in absolute value), use the decimals parameter to define the number of decimal digits; else, use the precision parameter to define the number of decimal digits.

A few examples (consider decimals being 4 and precision being 8):

Originally authored by Julien Aymé.


Method Summary
static void formatDouble(double source, int decimals, int precision, StringBuffer target)
          Rounds the given source value at the given precision and writes the rounded value into the given target
static void formatDoubleFast(double source, int decimals, int precision, StringBuffer target)
          Rounds the given source value at the given precision and writes the rounded value into the given target
static void formatDoublePrecise(double source, int decimals, int precision, StringBuffer target)
          Rounds the given source value at the given precision and writes the rounded value into the given target
static int getExponant(double value)
          Returns the exponent of the given value
static long tenPow(int n)
          Returns ten to the power of n
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

formatDouble

public static void formatDouble(double source,
                                int decimals,
                                int precision,
                                StringBuffer target)
Rounds the given source value at the given precision and writes the rounded value into the given target

Parameters:
source - the source value to round
decimals - the decimals to round at (use if abs(source) ≥ 1.0)
precision - the precision to round at (use if abs(source) < 1.0)
target - the buffer to write to

formatDoublePrecise

public static void formatDoublePrecise(double source,
                                       int decimals,
                                       int precision,
                                       StringBuffer target)
Rounds the given source value at the given precision and writes the rounded value into the given target

This method internally uses the String representation of the source value, in order to avoid any double precision computation error.

Parameters:
source - the source value to round
decimals - the decimals to round at (use if abs(source) ≥ 1.0)
precision - the precision to round at (use if abs(source) < 1.0)
target - the buffer to write to

tenPow

public static long tenPow(int n)
Returns ten to the power of n

Parameters:
n - the nth power of ten to get
Returns:
ten to the power of n

formatDoubleFast

public static void formatDoubleFast(double source,
                                    int decimals,
                                    int precision,
                                    StringBuffer target)
Rounds the given source value at the given precision and writes the rounded value into the given target

This method internally uses double precision computation and rounding, so the result may not be accurate (see formatDouble method for conditions).

Parameters:
source - the source value to round
decimals - the decimals to round at (use if abs(source) ≥ 1.0)
precision - the precision to round at (use if abs(source) < 1.0)
target - the buffer to write to

getExponant

public static int getExponant(double value)
Returns the exponent of the given value

Parameters:
value - the value to get the exponent from
Returns:
the value's exponent

xmlgraphics-commons 2.1

Copyright 1999-2016 The Apache Software Foundation. All Rights Reserved.