com.ibm.icu.math.BigDecimal Java Examples

The following examples show how to use com.ibm.icu.math.BigDecimal. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PluralRulesObject.java    From es6draft with MIT License 6 votes vote down vote up
private NumberFormat createNumberFormat() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    locale = locale.setKeywordValue("numbers", "latn");

    DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale, NumberFormat.NUMBERSTYLE);
    if (minimumSignificantDigits != 0 && maximumSignificantDigits != 0) {
        numberFormat.setSignificantDigitsUsed(true);
        numberFormat.setMinimumSignificantDigits(minimumSignificantDigits);
        numberFormat.setMaximumSignificantDigits(maximumSignificantDigits);
    } else {
        numberFormat.setSignificantDigitsUsed(false);
        numberFormat.setMinimumIntegerDigits(minimumIntegerDigits);
        numberFormat.setMinimumFractionDigits(minimumFractionDigits);
        numberFormat.setMaximumFractionDigits(maximumFractionDigits);
    }
    // as required by ToRawPrecision/ToRawFixed
    numberFormat.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    return numberFormat;
}
 
Example #2
Source File: RuleBasedNumberFormat.java    From fitnotifications with Apache License 2.0 6 votes vote down vote up
/**
 * Bottleneck through which all the public format() methods
 * that take a double pass. By the time we get here, we know
 * which rule set we're using to do the formatting.
 * @param number The number to format
 * @param ruleSet The rule set to use to format the number
 * @return The text that resulted from formatting the number
 */
private String format(double number, NFRuleSet ruleSet) {
    // all API format() routines that take a double vector through
    // here.  Create an empty string buffer where the result will
    // be built, and pass it to the rule set (along with an insertion
    // position of 0 and the number being formatted) to the rule set
    // for formatting
    StringBuilder result = new StringBuilder();
    if (getRoundingMode() != BigDecimal.ROUND_UNNECESSARY) {
        // We convert to a string because BigDecimal insists on excessive precision.
        number = new BigDecimal(Double.toString(number)).setScale(getMaximumFractionDigits(), roundingMode).doubleValue();
    }
    ruleSet.format(number, result, 0, 0);
    postProcess(result, ruleSet);
    return result.toString();
}
 
Example #3
Source File: DifferenceEntry.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Number[] getNumberData( )
{
	if ( isBigNumber )
	{
		return new BigNumber[]{bnPosValue, bnNegValue};
	}
	else if ( isBigDecimal )
	{
		if ( bdPosValue instanceof BigDecimal )
		{
			return new BigDecimal[]{
					(BigDecimal) bdPosValue, (BigDecimal) bdNegValue
			};
		}
		return new java.math.BigDecimal[]{
				(java.math.BigDecimal) bdPosValue,
				(java.math.BigDecimal) bdNegValue
		};
	}
	return new Double[]{
			Double.valueOf( dPosValue ), Double.valueOf( dNegValue )
	};
}
 
Example #4
Source File: AxisSubUnit.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public final double valuePercentage( double dValue )
{
	if ( dPositiveTotal - dNegativeTotal == 0 )
	{
		return 0;
	}
	// Do not use dTotal to compute percentage to avoid data out of bound
	double result = ( dValue * 100d ) / ( dPositiveTotal - dNegativeTotal );
	// If result is out of double, then use big decimal to compute.
	if ( Double.isInfinite( result ) || Double.isNaN( result ) )
	{
		result = BigDecimal.valueOf( dValue )
				.multiply( BigDecimal.valueOf( 100 ),
						NumberUtil.DEFAULT_MATHCONTEXT )
				.divide( BigDecimal.valueOf( dPositiveTotal
						- dNegativeTotal ),
						NumberUtil.DEFAULT_MATHCONTEXT )
				.doubleValue( );
	}
	return result;
}
 
Example #5
Source File: AxesRenderHelper.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public void handlePostEachTick( int i ) throws ChartException
{
	if ( i == da.size( ) - 2 && !sc.isSetFactor( ) )
	{
		// This is the last tick, use pre-computed value to
		// handle non-equal scale unit case.
		dAxisValue = Methods.asDouble( sc.getMaximum( ) ).doubleValue( );
	}
	else
	{
		dAxisValue += dAxisStep;
	}
	
	if ( sc.isBigNumber( ) )
	{
		bdAxisValue = BigDecimal.valueOf( dAxisValue )
				.multiply( sc.getBigNumberDivisor( ),
						NumberUtil.DEFAULT_MATHCONTEXT );
	}

}
 
Example #6
Source File: AxesRenderHelper.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public void initialize( ) throws ChartException
{
	dAxisValue = Methods.asDouble( sc.getMinimum( ) ).doubleValue( );
	dAxisStep = Methods.asDouble( sc.getStep( ) ).doubleValue( );
	
	if ( sc.isBigNumber( ) )
	{
		bdAxisValue = BigDecimal.valueOf( dAxisValue )
				.multiply( sc.getBigNumberDivisor( ),
						NumberUtil.DEFAULT_MATHCONTEXT );
		bdAxisStep = BigDecimal.valueOf( dAxisStep );
		if ( axModel.getFormatSpecifier( ) == null )
		{
			df = sc.computeDecimalFormat( bdAxisValue, bdAxisStep.multiply( sc.getBigNumberDivisor( ), NumberUtil.DEFAULT_MATHCONTEXT ) );
		}
	}
	else
	{
		if ( axModel.getFormatSpecifier( ) == null )
		{
			df = sc.computeDecimalFormat( dAxisValue, dAxisStep );
		}
	}
}
 
Example #7
Source File: JavaNumberFormatSpecifierImpl.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public String format( Number number, ULocale lo )
{
	Number n = NumberUtil.transformNumber( number );
	if ( n instanceof Double )
	{
		return format( ( (Double) n ).doubleValue( ), lo );
	}

	// Format big decimal
	BigDecimal bdNum = (BigDecimal) n;
	final DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance( lo );
	String vPattern = NumberUtil.adjustBigNumberFormatPattern( getPattern( ) );
	if ( vPattern.indexOf( 'E' ) < 0 )
	{
		vPattern = vPattern + NumberUtil.BIG_DECIMAL_FORMAT_SUFFIX;
	}
	df.applyPattern( vPattern );
	return isSetMultiplier( ) ? df.format( bdNum.multiply( BigDecimal.valueOf( getMultiplier( ) ),
			NumberUtil.DEFAULT_MATHCONTEXT ) )
			: df.format( bdNum );
}
 
Example #8
Source File: NumberUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This method transform number value into Double or BigDecimal. If
 * specified number is Double, Float, Integer, Long, Short, Unsigned Int,
 * Unsigned Long, Byte, Char. It will be transformed as Double. If specified
 * number is BigDecimal or BigInteger. It will be transformed as BigDecimal.
 * 
 * @param n
 * @return
 */
public static Number transformNumber( Object n )
{
	if ( n == null )
	{
		return null;
	}
	else if ( n instanceof Double )
	{
		return (Double)n;
	}
	else if ( n instanceof BigDecimal )
	{
		return (BigDecimal)n;
	}
	else if ( n instanceof java.math.BigDecimal )
	{
		return  new BigDecimal(( (java.math.BigDecimal) n ).toString( ) );
	}
	else if ( n instanceof BigInteger )
	{
		return new BigDecimal((BigInteger)n); 
	}

	return Methods.asDouble( n );
}
 
Example #9
Source File: NumberUtil.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * This method wraps number as big decimal.
 * 
 * @param n
 * @return
 */
public static BigDecimal asBigDecimal( Number n )
{
	if ( n == null )
	{
		return null;
	}
	else if ( n instanceof BigNumber )
	{
		return ((BigNumber)n).getValue( );
	}
	else if ( n instanceof BigDecimal )
	{
		return (BigDecimal) n;
	}
	else if ( n instanceof java.math.BigDecimal )
	{
		return  new BigDecimal(( (java.math.BigDecimal) n ).toString( ) );
	}
	else if ( n instanceof BigInteger )
	{
		return new BigDecimal( n.toString( ) );
	}
	
	return BigDecimal.valueOf( n.doubleValue( ) );
}
 
Example #10
Source File: AxesRenderHelper.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public void initialize( ) throws ChartException
{
	dAxisValue = Methods.asDouble( sc.getMinimum( ) ).doubleValue( );
	dAxisStep = Methods.asDouble( sc.getStep( ) ).doubleValue( );
	if ( sc.isBigNumber( ) )
	{
		bdAxisValue = BigDecimal.valueOf( dAxisValue ).multiply( sc.getBigNumberDivisor( ), NumberUtil.DEFAULT_MATHCONTEXT );
		bdAxisStep = BigDecimal.valueOf( dAxisStep );
		if ( axModel.getFormatSpecifier( ) == null )
		{
			df = sc.computeDecimalFormat( bdAxisValue, bdAxisStep );
		}
	}
	else
	{
		if ( axModel.getFormatSpecifier( ) == null )
		{
			df = sc.computeDecimalFormat( dAxisValue, dAxisStep );
		}
	}
}
 
Example #11
Source File: BubbleEntry.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Number[] getNumberData( )
{
	if ( bIsBigNumber )
	{
		return new BigNumber[]{(BigNumber) oValue, bnSize};
	}
	else if ( bIsBigDecimal )
	{
		if ( oValue instanceof BigDecimal )
		{
			return new BigDecimal[]{ (BigDecimal) oValue, (BigDecimal) bdSize };
		}
		return new java.math.BigDecimal[]{ (java.math.BigDecimal) oValue, (java.math.BigDecimal) bdSize };
	}
	
	if ( oValue instanceof Number )
	{
		return new Double[]{ Double.valueOf( ((Number)oValue).doubleValue( ) ), Double.valueOf( dSize ) };
	}
	return null;
}
 
Example #12
Source File: MeasureFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * Create a format from the locale, formatWidth, and format.
 *
 * @param locale the locale.
 * @param formatWidth hints how long formatted strings should be.
 * @param format This is defensively copied.
 * @return The new MeasureFormat object.
 * @stable ICU 53
 */
public static MeasureFormat getInstance(ULocale locale, FormatWidth formatWidth, NumberFormat format) {
    PluralRules rules = PluralRules.forLocale(locale);
    NumericFormatters formatters = null;
    MeasureFormatData data = localeMeasureFormatData.get(locale);
    if (data == null) {
        data = loadLocaleData(locale);
        localeMeasureFormatData.put(locale, data);
    }
    if (formatWidth == FormatWidth.NUMERIC) {
        formatters = localeToNumericDurationFormatters.get(locale);
        if (formatters == null) {
            formatters = loadNumericFormatters(locale);
            localeToNumericDurationFormatters.put(locale, formatters);
        }
    }
    NumberFormat intFormat = NumberFormat.getInstance(locale);
    intFormat.setMaximumFractionDigits(0);
    intFormat.setMinimumFractionDigits(0);
    intFormat.setRoundingMode(BigDecimal.ROUND_DOWN);
    return new MeasureFormat(
            locale,
            data,
            formatWidth,
            new ImmutableNumberFormat(format),
            rules,
            formatters,
            new ImmutableNumberFormat(NumberFormat.getInstance(locale, formatWidth.getCurrencyStyle())),
            new ImmutableNumberFormat(intFormat));
}
 
Example #13
Source File: NumberFormatObject.java    From es6draft with MIT License 5 votes vote down vote up
private NumberFormat createNumberFormat() {
    ULocale locale = ULocale.forLanguageTag(this.locale);
    int choice;
    if ("decimal".equals(style)) {
        choice = NumberFormat.NUMBERSTYLE;
    } else if ("percent".equals(style)) {
        choice = NumberFormat.PERCENTSTYLE;
    } else {
        if ("code".equals(currencyDisplay)) {
            choice = NumberFormat.ISOCURRENCYSTYLE;
        } else if ("symbol".equals(currencyDisplay)) {
            choice = NumberFormat.CURRENCYSTYLE;
        } else {
            choice = NumberFormat.PLURALCURRENCYSTYLE;
        }
    }
    DecimalFormat numberFormat = (DecimalFormat) NumberFormat.getInstance(locale, choice);
    if ("currency".equals(style)) {
        numberFormat.setCurrency(Currency.getInstance(currency));
    }
    // numberingSystem is already handled in language-tag
    // assert locale.getKeywordValue("numbers").equals(numberingSystem);
    if (minimumSignificantDigits != 0 && maximumSignificantDigits != 0) {
        numberFormat.setSignificantDigitsUsed(true);
        numberFormat.setMinimumSignificantDigits(minimumSignificantDigits);
        numberFormat.setMaximumSignificantDigits(maximumSignificantDigits);
    } else {
        numberFormat.setSignificantDigitsUsed(false);
        numberFormat.setMinimumIntegerDigits(minimumIntegerDigits);
        numberFormat.setMinimumFractionDigits(minimumFractionDigits);
        numberFormat.setMaximumFractionDigits(maximumFractionDigits);
    }
    numberFormat.setGroupingUsed(useGrouping);
    // as required by ToRawPrecision/ToRawFixed
    // FIXME: ICU4J bug:
    // new Intl.NumberFormat("en",{useGrouping:false}).format(111111111111111)
    // returns "111111111111111.02"
    numberFormat.setRoundingMode(BigDecimal.ROUND_HALF_UP);
    return numberFormat;
}
 
Example #14
Source File: NumberUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method wraps number as big number.
 * 
 * @param n
 * @param divisor
 * @return
 */
public static BigNumber asBigNumber( Number n, BigDecimal divisor )
{
	if ( n == null )
	{
		return null;
	}
	else if ( n instanceof Double )
	{
		if ( ( (Double) n ).isNaN( ) )
		{
			return new BigNumber(BigDecimal.ZERO, divisor ); 
		}
		return new BigNumber(BigDecimal.valueOf( ((Double)n ).doubleValue( )), divisor );
	}
	else if ( n instanceof BigDecimal )
	{
		return new BigNumber( (BigDecimal)n, divisor );
	}
	else if ( n instanceof java.math.BigDecimal )
	{
		return new BigNumber( asBigDecimal( n ), divisor );
	}
	else if ( n instanceof BigNumber )
	{
		((BigNumber)n).setDivisor( divisor );
		return (BigNumber) n;
	}
	
	return new BigNumber(BigDecimal.valueOf( n.doubleValue( )), divisor );
}
 
Example #15
Source File: ChartUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
private static void adjustDataSets( Axis ax ) throws ChartException
{
	List<Series> seriesList = new ArrayList<Series>( );
	for ( SeriesDefinition sd: ax.getSeriesDefinitions( ) )
	{
		seriesList.addAll( sd.getRunTimeSeries( ) );
	}
	
	BigDecimal bnMin = NumberUtil.asBigDecimal( NumberUtil.convertNumber( ax.getScale( ).getMin( )) );
	BigDecimal bnMax = NumberUtil.asBigDecimal( NumberUtil.convertNumber( ax.getScale( ).getMax( )) );
	adjustDataSets( seriesList.toArray( new Series[]{} ), bnMin, bnMax );
}
 
Example #16
Source File: ChartUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Checks precise of big number. 
 * 
 * @param bdValue
 * @return precise or not
 * @since 2.6
 */
public static boolean checkBigNumberPrecise( BigDecimal bdValue )
{
	if ( bdValue.compareTo( BigDecimal.valueOf( bdValue.intValue( ) )) == 0 )
	{
		return true;
	}
	
	final DecimalFormatSymbols dfs = new DecimalFormatSymbols( );
	String sValue = String.valueOf( bdValue );
	int iEPosition = sValue.indexOf( dfs.getExponentSeparator( ) );

	if ( iEPosition > 0 )
	{
		sValue = sValue.substring( 0, iEPosition );
	}
	
	if ( sValue.length( ) < 8 )
	{
		return true;
	}
	int iPoint = sValue.indexOf( '.' );
	int iZero = sValue.lastIndexOf( "00000000" ); //$NON-NLS-1$
	if ( iZero >= iPoint && iEPosition < 0 )
	{
		return false;
	}
	int iNine = sValue.lastIndexOf( "99999999" ); //$NON-NLS-1$
	if ( iNine >= iPoint && iEPosition < 0 )
	{
		return false;
	}
	return true;
}
 
Example #17
Source File: RuleBasedNumberFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * <strong style="font-family: helvetica; color: red;">NEW</strong>
 * Implement com.ibm.icu.text.NumberFormat:
 * Format a BigInteger.
 * @stable ICU 2.0
 */
@Override
public StringBuffer format(BigInteger number,
                           StringBuffer toAppendTo,
                           FieldPosition pos) {
    return format(new com.ibm.icu.math.BigDecimal(number), toAppendTo, pos);
}
 
Example #18
Source File: HTMLActionHandler.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Get display value.
 * 
 * @param value
 * @return
 */
String getDisplayValue( Object value )
{
	if ( value == null )
		return null;

	if ( value instanceof Float || value instanceof Double
			|| value instanceof BigDecimal )
	{
		return value.toString( );
	}
	return ParameterValidationUtil.getDisplayValue( value );
}
 
Example #19
Source File: DrbImageManager.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public BigDecimal getNextEasting (BigDecimal current)
{
   if (current == null) return getFirstEasting();
   Iterator<BigDecimal>it = eastings.iterator();
   while (it.hasNext())
   {
      BigDecimal cursor = it.next();
      if (cursor.equals(current))
      {
         if (it.hasNext()) return it.next();
         else return null;
      }
   }
   return null;
}
 
Example #20
Source File: DrbImageManager.java    From DataHubSystem with GNU Affero General Public License v3.0 5 votes vote down vote up
public BigDecimal getNextNorthing (BigDecimal current)
{
   if (current == null) return getFirstNorthing();
   Iterator<BigDecimal>it = northings.descendingIterator();
   while (it.hasNext())
   {
      BigDecimal cursor = it.next();
      if (cursor.equals(current))
      {
         if (it.hasNext()) return it.next();
         else return null;
      }
   }
   return null;
}
 
Example #21
Source File: RuleBasedNumberFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * <strong style="font-family: helvetica; color: red;">NEW</strong>
 * Implement com.ibm.icu.text.NumberFormat:
 * Format a BigDecimal.
 * @stable ICU 2.0
 */
@Override
public StringBuffer format(java.math.BigDecimal number,
                           StringBuffer toAppendTo,
                           FieldPosition pos) {
    return format(new com.ibm.icu.math.BigDecimal(number), toAppendTo, pos);
}
 
Example #22
Source File: BigNumber.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sets divisor.
 * 
 * @param divisor
 */
public void setDivisor( BigDecimal divisor )
{
	this.divisor = divisor;
	doublePart = value.divide( divisor, NumberUtil.DEFAULT_MATHCONTEXT )
			.doubleValue( );
	if ( Double.isNaN( doublePart ) || Double.isInfinite( doublePart ) )
	{
		doublePart = 0d;
	}
}
 
Example #23
Source File: BigNumber.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Constructs an instance of BigNumber with divisor.
 * 
 * @param value
 * @param divisor
 */
public BigNumber( BigDecimal value, BigDecimal divisor )
{
	this.value = value;
	if ( divisor != null )
	{
		setDivisor( divisor );
	}
}
 
Example #24
Source File: NumberFormatSpecifierImpl.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
public String format( Number number, ULocale lo )
{
	Number n = NumberUtil.transformNumber( number );
	if ( n instanceof Double )
	{
		return format( ( (Double) number ).doubleValue( ), lo );
	}

	// Format big decimal.
	BigDecimal bdNum = (BigDecimal) n;
	final DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance( lo );
	if ( isSetFractionDigits( ) )
	{
		df.setMinimumFractionDigits( getFractionDigits( ) );
		df.setMaximumFractionDigits( getFractionDigits( ) );
	}
	String pattern = NumberUtil.adjustBigNumberFormatPattern( df.toLocalizedPattern( ) );
	if ( pattern.indexOf( 'E' ) < 0 )
	{
		pattern = pattern + NumberUtil.BIG_DECIMAL_FORMAT_SUFFIX;
	}

	df.applyLocalizedPattern( pattern );

	final StringBuffer sb = new StringBuffer( );
	if ( getPrefix( ) != null )
	{
		sb.append( getPrefix( ) );
	}
	sb.append( isSetMultiplier( ) ? df.format( bdNum.multiply( BigDecimal.valueOf( getMultiplier( ) ),
			NumberUtil.DEFAULT_MATHCONTEXT ) )
			: df.format( bdNum ) );
	if ( getSuffix( ) != null )
	{
		sb.append( getSuffix( ) );
	}

	return sb.toString( );
}
 
Example #25
Source File: RuleBasedNumberFormat.java    From fitnotifications with Apache License 2.0 5 votes vote down vote up
/**
 * <strong style="font-family: helvetica; color: red;">NEW</strong>
 * Implement com.ibm.icu.text.NumberFormat:
 * Format a BigDecimal.
 * @stable ICU 2.0
 */
@Override
public StringBuffer format(com.ibm.icu.math.BigDecimal number,
                           StringBuffer toAppendTo,
                           FieldPosition pos) {
    if (MIN_VALUE.compareTo(number) >= 0 || MAX_VALUE.compareTo(number) <= 0) {
        // We're outside of our normal range that this framework can handle.
        // The DecimalFormat will provide more accurate results.
        return getDecimalFormat().format(number, toAppendTo, pos);
    }
    if (number.scale() == 0) {
        return format(number.longValue(), toAppendTo, pos);
    }
    return format(number.doubleValue(), toAppendTo, pos);
}
 
Example #26
Source File: NumberFormatService.java    From singleton with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Format a number to localized number by scale
 * @param locale
 * @param number
 * @param scale
 * @return Localized number
 */
public String formatNumber(String locale, String number, int scale) {
    Number num = this.parseNumber(number);
	ULocale uLocale = new ULocale(locale);
	NumberFormat numberFormat = NumberFormat.getNumberInstance(uLocale);
	numberFormat.setMaximumFractionDigits(scale);
	numberFormat.setMinimumFractionDigits(scale);
	numberFormat.setRoundingMode(BigDecimal.ROUND_HALF_UP);
	return numberFormat.format(num);
}
 
Example #27
Source File: AutoScale.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
AxisValueProvider(double dAxisValue, double dAxisStep, AutoScale as, ScaleInfo si )
{
	this.dAxisValue = dAxisValue;
	this.dAxisStep = dAxisStep;
	this.as = as;
	this.si = si;
	if ( as.isBigNumber( ) )
	{
		this.bdAxisValue = BigDecimal.valueOf( dAxisValue ).multiply( as.getBigNumberDivisor( ),
				NumberUtil.DEFAULT_MATHCONTEXT );
		this.bdAxisStep = BigDecimal.valueOf( dAxisStep );
		this.bdStep = bdAxisStep.multiply( as.getBigNumberDivisor( ),
				NumberUtil.DEFAULT_MATHCONTEXT );
	}	
}
 
Example #28
Source File: AutoScale.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns an actual value object according to current argument and
 * contenxt.
 * 
 * @param originValue
 * @param divisor the divisor used for big number case.
 * @return
 */
static Object getValue(Object originValue, BigDecimal divisor )
{
	if ( originValue instanceof Number )
	{
		if ( divisor != null )
		{
			return NumberUtil.asBigDecimal( (Number) originValue )
					.multiply( divisor, NumberUtil.DEFAULT_MATHCONTEXT );
		}
		return ( (Number) originValue ).doubleValue( );
	}
	else if ( originValue instanceof NumberDataElement )
	{
		Double d = new Double( ( (NumberDataElement) originValue ).getValue( ) );
		if ( divisor != null )
		{
			return NumberUtil.asBigDecimal( d ).multiply( divisor,
					NumberUtil.DEFAULT_MATHCONTEXT );
		}
		return d;
	}
	else if ( originValue instanceof BigNumberDataElementImpl )
	{
		return ((BigNumberDataElementImpl)originValue).getValue( );
	}
	return originValue;
}
 
Example #29
Source File: AutoScale.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Sets big number divisor for axis scale.
 * 
 * @param divisor
 * @since 2.6
 */
public void setBigNubmerDivisor(BigDecimal divisor)
{
	if ( divisor == null )
	{
		this.bIsBigNumber = false;
		this.bigNumberDivisor = null;
	}
	else
	{
		this.bIsBigNumber = true;
		this.bigNumberDivisor = divisor;
	}
}
 
Example #30
Source File: NumberUtil.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * This method convert number to Double or BigDecimal types.
 * 
 * @param n
 * @return
 */
public static Number convertNumber( Object n )
{
	if ( n == null )
	{
		return null;
	}
	else if ( n instanceof Double )
	{
		return (Double)n;
	}
	else if ( n instanceof BigDecimal )
	{
		return (BigDecimal)n;
	}
	else if ( n instanceof java.math.BigDecimal )
	{
		return  (java.math.BigDecimal)n;
	}
	else if ( n instanceof BigInteger )
	{
		return new BigDecimal((BigInteger)n); 
	}
	else if (n instanceof NumberDataElement)
	{
		return ( (NumberDataElement) n ).getValue( );
	}
	else if (n instanceof BigNumberDataElement)
	{
		return ( (BigNumberDataElement) n ).getValue( );
	}

	return Methods.asDouble( n );
}