Java Code Examples for com.ibm.icu.text.NumberFormat#parse()

The following examples show how to use com.ibm.icu.text.NumberFormat#parse() . 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: RadarLineSheet.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private double getTypedDataElement( String strDataElement )
{
	if ( strDataElement.trim( ).length( ) == 0 )
	{
		return 0.0;
	}
	NumberFormat nf = ChartUIUtil.getDefaultNumberFormatInstance( );

	try
	{
		Number numberElement = nf.parse( strDataElement );
		return numberElement.doubleValue( );
	}
	catch ( ParseException e1 )
	{
		return 0.0;
	}
}
 
Example 2
Source File: NumberDataElementComposite.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public DataElement getDataElement( )
{
	NumberFormat nf = ChartUIUtil.getDefaultNumberFormatInstance( );
	try
	{
		Number number = nf.parse( getText( ) );
		if (number instanceof BigInteger)
		{
			BigInteger biNumber = (BigInteger)number;
			return BigNumberDataElementImpl.create( new BigDecimal( biNumber ).stripTrailingZeros( ) );
		}
		return NumberDataElementImpl.create( number.doubleValue( ) );
	}
	catch ( ParseException e1 )
	{
		return null;
	}
}
 
Example 3
Source File: SeriesRegionSheet.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
private DataElement getTypedDataElement( String strDataElement )
{
	if ( strDataElement == null )
	{
		return null;
	}
	if ( strDataElement.trim( ).length( ) == 0 )
	{
		return NumberDataElementImpl.create( 0.0 );
	}
	NumberFormat nf = ChartUIUtil.getDefaultNumberFormatInstance( );

	try
	{
		Number numberElement = nf.parse( strDataElement );
		return NumberDataElementImpl.create( numberElement.doubleValue( ) );
	}
	catch ( ParseException e1 )
	{
		return NumberDataElementImpl.create( 0.0 );
	}
}
 
Example 4
Source File: ValueFormatter.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Normalize double value to avoid error precision.
 * 
 * @param value
 * @return normalized value of specified double.
 */
public static Number normalizeDouble( Double value )
{
	if ( value.isNaN( ) )
	{
		return 0;
	}
	
	NumberFormat df = createDefaultNumberFormat( value, ULocale.ENGLISH );

	// Normalize double value to avoid double precision error.
	String sValue = df.format( value );
	try
	{
		return df.parse( sValue );
	}
	catch ( ParseException e )
	{
		logger.log( e );
	}
	
	return value;
}
 
Example 5
Source File: ValueFormatter.java    From birt with Eclipse Public License 1.0 6 votes vote down vote up
public static Number normalizeDouble( Double dValue, String pattern )
{
	Number value = null;
	if ( pattern != null && pattern.trim( ).length( ) > 0 )
	{
		NumberFormat df = new DecimalFormat( pattern );

		String sValue = df.format( dValue );

		try
		{
			value = df.parse( sValue );
		}
		catch ( ParseException e )
		{
			logger.log( e );;
		}

	}
	else
	{
		value = normalizeDouble( dValue );
	}
	return value;
}
 
Example 6
Source File: FormatterImpl.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
public Number parseNumber(String image, final boolean lenient) {
	image = image.trim();
	if (!image.isEmpty()) {
		String toParse = image;
		if (toParse.length() > 1 && toParse.charAt(0) == '+')
			toParse = toParse.substring(1);
		NumberFormat nf = NumberFormat.getNumberInstance(iLocale);
		ParsePosition p = new ParsePosition(0);
		Number ret = nf.parse(toParse, p);
		int errIndex = p.getErrorIndex();
		//System.out.println("Ind=" + index + " ErrInd=" + errIndex);
		if (errIndex == -1) {
			if (p.getIndex() >= toParse.length() || lenient)
				return ret;
		} else if (errIndex != 0 && lenient)
			return ret;
	}
	throw new IllegalArgumentException("Illegal number string '" + image + "'");
}
 
Example 7
Source File: DataDefinitionSelector.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private String convertDataSetRepresentation( String dsRepresentation,
		int seriesDefinitionIndex )
{
	if ( dsRepresentation != null )
	{
		String[] strTok = ChartUtil.getStringTokens( dsRepresentation );
		StringBuffer sb = new StringBuffer( );
		for ( int i = 0; i < strTok.length; i++ )
		{
			String strDataElement = strTok[i];
			SimpleDateFormat sdf = new SimpleDateFormat( "MM/dd/yyyy" ); //$NON-NLS-1$
			NumberFormat nf = NumberFormat.getNumberInstance( );

			try
			{
				Date dateElement = sdf.parse( strDataElement );
				dateElement.setTime( dateElement.getTime( )
						+ ( dateElement.getTime( ) * seriesDefinitionIndex )
						/ 10 );
				sb.append( sdf.format( dateElement ) );
			}
			catch ( ParseException e )
			{
				try
				{
					Number numberElement = nf.parse( strDataElement );
					sb.append( numberElement.doubleValue( )
							* ( seriesDefinitionIndex + 1 ) );
				}
				catch ( ParseException e1 )
				{
					e1.printStackTrace( );
				}
			}
			if ( i < strTok.length - 1 )
			{
				sb.append( "," ); //$NON-NLS-1$
			}

		}
		return sb.toString( );
	}
	return null;
}