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

The following examples show how to use com.ibm.icu.text.NumberFormat#getNumberInstance() . 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: 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 2
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 3
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;
}
 
Example 4
Source File: StockSeriesImpl.java    From birt with Eclipse Public License 1.0 4 votes vote down vote up
private String getConvertedOrthogonalSampleDataRepresentation(
		String sOldRepresentation )
{
	StringTokenizer strtok = new StringTokenizer( sOldRepresentation, "," ); //$NON-NLS-1$
	NumberFormat nf = NumberFormat.getNumberInstance( );
	StringBuffer sbNewRepresentation = new StringBuffer( "" ); //$NON-NLS-1$
	int iValueCount = 0;
	while ( strtok.hasMoreTokens( ) )
	{
		String sElement = strtok.nextToken( ).trim( );
		try
		{
			if ( nf.parse( sElement ).doubleValue( ) < 0 )
			{
				// If the value is negative, use an arbitrary positive value
				sElement = String.valueOf( 4.0 + iValueCount );
				iValueCount++;
			}
		}
		catch ( ParseException e )
		{
			sElement = String.valueOf( 4.0 + iValueCount );
			iValueCount++;
		}
		sbNewRepresentation.append( "H" ); //$NON-NLS-1$
		sbNewRepresentation.append( sElement );
		sbNewRepresentation.append( " " ); //$NON-NLS-1$

		sbNewRepresentation.append( " L" ); //$NON-NLS-1$
		sbNewRepresentation.append( sElement );
		sbNewRepresentation.append( " " ); //$NON-NLS-1$

		sbNewRepresentation.append( " O" ); //$NON-NLS-1$
		sbNewRepresentation.append( sElement );
		sbNewRepresentation.append( " " ); //$NON-NLS-1$

		sbNewRepresentation.append( " C" ); //$NON-NLS-1$
		sbNewRepresentation.append( sElement );
		sbNewRepresentation.append( "," ); //$NON-NLS-1$
	}
	return sbNewRepresentation.toString( ).substring( 0,
			sbNewRepresentation.length( ) - 1 );
}
 
Example 5
Source File: FormatterImpl.java    From org.openntf.domino with Apache License 2.0 4 votes vote down vote up
public String formatNumber(final Number n, final LotusNumberOptions lno) {
	NumberFormat nf;
	/*
	 * It would have been more convenient to use NumberFormat.getInstance(locale, style),
	 * but this method is private in com.ibm.icu_3.8.1.v20120530.jar.
	 * (Seems to be public as of ICU 4.2.)
	 */
	if (lno.format == 'C')
		nf = NumberFormat.getCurrencyInstance(iLocale);
	else if (lno.format == 'S')
		nf = NumberFormat.getScientificInstance(iLocale);
	else if (lno.format == '%')
		nf = NumberFormat.getPercentInstance(iLocale);
	else
		nf = NumberFormat.getNumberInstance(iLocale);
	nf.setGroupingUsed(lno.useGrouping);
	nf.setMaximumIntegerDigits(1000);
	if (lno.fractionDigits != -1) {
		nf.setMinimumFractionDigits(lno.fractionDigits);
		nf.setMaximumFractionDigits(lno.fractionDigits);
	} else
		nf.setMaximumFractionDigits(1000);
	String ret = nf.format(n);
	do {
		if (lno.format != 'G' || ret.length() <= 15)
			break;
		/*
		 * In this case, Lotus implicitly switches to scientific style.
		 * When useGrouping is in effect, the limit decreases from 15 to 12 in Lotus
		 * (i.e. the grouping bytes are likewise counted), but we are not going to
		 *  imitate this strange behaviour.
		 */
		String tester = ret;
		if (lno.useGrouping) {
			nf.setGroupingUsed(false);
			tester = nf.format(n);
		}
		int minus = (tester.charAt(0) == '-') ? 1 : 0;
		int lh = tester.length();
		if (lh - minus <= 15)
			break;
		int komma = minus;
		for (; komma < lh; komma++)
			if (!Character.isDigit(tester.charAt(komma)))
				break;
		if (komma - minus <= 15)
			break;
		nf = NumberFormat.getScientificInstance(iLocale);
		nf.setGroupingUsed(lno.useGrouping);
		ret = nf.format(n);
	} while (false);
	if (lno.negativeAsParentheses && ret.charAt(0) == '-')
		ret = '(' + ret.substring(1) + ')';
	return ret;
}