Java Code Examples for java.text.NumberFormat#getInstance()

The following examples show how to use java.text.NumberFormat#getInstance() . 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: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testDenominatorFormat() {
    NumberFormat old = properFormat.getDenominatorFormat();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setParseIntegerOnly(true);
    properFormat.setDenominatorFormat(nf);
    Assert.assertEquals(nf, properFormat.getDenominatorFormat());
    properFormat.setDenominatorFormat(old);

    old = improperFormat.getDenominatorFormat();
    nf = NumberFormat.getInstance();
    nf.setParseIntegerOnly(true);
    improperFormat.setDenominatorFormat(nf);
    Assert.assertEquals(nf, improperFormat.getDenominatorFormat());
    improperFormat.setDenominatorFormat(old);
}
 
Example 2
Source File: SitePermissionController.java    From webcurator with Apache License 2.0 6 votes vote down vote up
/**
 * Initialise some special binders for this command. (Overrides Spring
 * method).
 * @param request The HttpServletRequest.
 * @param binder  The binder.
 */
@Override
public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
	super.initBinder(request, binder);
	
       NumberFormat nf = NumberFormat.getInstance(request.getLocale());	
       
       // Register the binders.
       binder.registerCustomEditor(Long.class, "selectedPermission", new CustomNumberEditor(Long.class, nf, true));
	binder.registerCustomEditor(java.util.Date.class, "startDate", DateUtils.get().getFullDateEditor(true));
	binder.registerCustomEditor(java.util.Date.class, "endDate", DateUtils.get().getFullDateEditor(true));
	binder.registerCustomEditor(java.util.Date.class, "openAccessDate", DateUtils.get().getFullDateEditor(true));
	
	// If the session model is available, we want to register the Permission's
	// authorising agency editor.
	if(getEditorContext(request) != null) {
		binder.registerCustomEditor(AuthorisingAgent.class, "authorisingAgent", new EditorContextObjectEditor(getEditorContext(request), AuthorisingAgent.class));
		binder.registerCustomEditor(Set.class, "urls", new UrlPatternCollectionEditor(Set.class, true, getEditorContext(request)));
		binder.registerCustomEditor(Integer.class, "deleteExclusionIndex", new CustomNumberEditor(Integer.class, true));
	}
}
 
Example 3
Source File: NumberFormatTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * @tests java.text.NumberFormat#getCurrency()
 */
public void test_getCurrency() {
    // Test for method java.util.Currency getCurrency()

    // a subclass that supports currency formatting
    Currency currH = Currency.getInstance("HUF");
    NumberFormat format = NumberFormat.getInstance(new Locale("hu", "HU"));
    assertSame("Returned incorrect currency", currH, format.getCurrency());

    // a subclass that doesn't support currency formatting
    ChoiceFormat cformat = new ChoiceFormat(
            "0#Less than one|1#one|1<Between one and two|2<Greater than two");
    try {
        ((NumberFormat) cformat).getCurrency();
        fail("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
    }
}
 
Example 4
Source File: NumberStyleFormatter.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public NumberFormat getNumberFormat(Locale locale) {
	NumberFormat format = NumberFormat.getInstance(locale);
	if (!(format instanceof DecimalFormat)) {
		if (this.pattern != null) {
			throw new IllegalStateException("Cannot support pattern for non-DecimalFormat: " + format);
		}
		return format;
	}
	DecimalFormat decimalFormat = (DecimalFormat) format;
	decimalFormat.setParseBigDecimal(true);
	if (this.pattern != null) {
		decimalFormat.applyPattern(this.pattern);
	}
	return decimalFormat;
}
 
Example 5
Source File: Comman.java    From XERUNG with Apache License 2.0 6 votes vote down vote up
public String convertByteTo(long byteData){
	NumberFormat formatter = NumberFormat.getInstance();
	formatter.setMaximumFractionDigits(2);
	try {
		double kb = byteData/1024L;
		if(kb >= 1024){
			double mb = kb/1024L;
			if(mb >= 1024){
				double gb = mb/1024L;
				return formatter.format(gb) +" GB";
			}else
				return  formatter.format(mb)+" MB";
		}else{				
			return formatter.format(kb)+" KB";
		}

	} catch (Exception e) {
		// TODO: handle exception
		return String.valueOf(byteData);
	}
}
 
Example 6
Source File: WebTableFloatCellRenderer.java    From weblaf with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Returns {@link NumberFormat}.
 *
 * @return {@link NumberFormat}
 */
protected NumberFormat getNumberFormat ()
{
    if ( numberFormat == null )
    {
        numberFormat = NumberFormat.getInstance ();
    }
    return numberFormat;
}
 
Example 7
Source File: TezClient.java    From incubator-tez with Apache License 2.0 5 votes vote down vote up
@Override
public NumberFormat initialValue() {
  NumberFormat fmt = NumberFormat.getInstance();
  fmt.setGroupingUsed(false);
  fmt.setMinimumIntegerDigits(6);
  return fmt;
}
 
Example 8
Source File: RealVectorFormatAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setUp() throws Exception {
    realVectorFormat = RealVectorFormat.getInstance(getLocale());
    final NumberFormat nf = NumberFormat.getInstance(getLocale());
    nf.setMaximumFractionDigits(2);
    realVectorFormatSquare = new RealVectorFormat("[", "]", " : ", nf);
}
 
Example 9
Source File: SynthTableUI.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
private void configureValue(Object value, Class columnClass) {
    if (columnClass == Object.class || columnClass == null) {
        setHorizontalAlignment(JLabel.LEADING);
    } else if (columnClass == Float.class || columnClass == Double.class) {
        if (numberFormat == null) {
            numberFormat = NumberFormat.getInstance();
        }
        setHorizontalAlignment(JLabel.TRAILING);
        setText((value == null) ? "" : ((NumberFormat)numberFormat).format(value));
    }
    else if (columnClass == Number.class) {
        setHorizontalAlignment(JLabel.TRAILING);
        // Super will have set value.
    }
    else if (columnClass == Icon.class || columnClass == ImageIcon.class) {
        setHorizontalAlignment(JLabel.CENTER);
        setIcon((value instanceof Icon) ? (Icon)value : null);
        setText("");
    }
    else if (columnClass == Date.class) {
        if (dateFormat == null) {
            dateFormat = DateFormat.getDateInstance();
        }
        setHorizontalAlignment(JLabel.LEADING);
        setText((value == null) ? "" : ((Format)dateFormat).format(value));
    }
    else {
        configureValue(value, columnClass.getSuperclass());
    }
}
 
Example 10
Source File: BigFractionFormatTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testWholeFormat() {
    ProperBigFractionFormat format = (ProperBigFractionFormat)properFormat;

    NumberFormat old = format.getWholeFormat();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setParseIntegerOnly(true);
    format.setWholeFormat(nf);
    Assert.assertEquals(nf, format.getWholeFormat());
    format.setWholeFormat(old);
}
 
Example 11
Source File: ClassDialogue.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public static JFormattedTextField createNumberField(Class<?> type, Object minValue, Object maxValue) {
  NumberFormat format = NumberFormat.getInstance();
  format.setGroupingUsed(false);
  format.setMaximumFractionDigits(10);
  NumberFormatter formatter = new NumberFormatter(format);
  formatter.setValueClass(type);
  formatter.setMinimum((Comparable) minValue);
  formatter.setMaximum((Comparable) maxValue);
  formatter.setAllowsInvalid(false);
  formatter.setCommitsOnValidEdit(true);
  formatter.setOverwriteMode(true);
  JFormattedTextField jftf = new JFormattedTextField(formatter);
  return jftf;
}
 
Example 12
Source File: ComplexFormatAbstractTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testGetImaginaryFormat() {
    NumberFormat nf = NumberFormat.getInstance();
    ComplexFormat cf = new ComplexFormat();

    assertNotSame(nf, cf.getImaginaryFormat());
    cf.setImaginaryFormat(nf);
    assertSame(nf, cf.getImaginaryFormat());
}
 
Example 13
Source File: UnivariateStatsDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Copy information from the meta-data m_currentMeta to the dialog fields.
 */
public void getData() {

  if ( m_currentMeta.getInputFieldMetaFunctions() != null ) {
    for ( int i = 0; i < m_currentMeta.getNumFieldsToProcess(); i++ ) {
      UnivariateStatsMetaFunction fn = m_currentMeta.getInputFieldMetaFunctions()[i];

      TableItem item = m_wFields.table.getItem( i );

      item.setText( 1, Const.NVL( fn.getSourceFieldName(), "" ) );
      item.setText( 2, Const.NVL( ( fn.getCalcN() ) ? "True" : "False", "" ) );
      item.setText( 3, Const.NVL( ( fn.getCalcMean() ) ? "True" : "False", "" ) );
      item.setText( 4, Const.NVL( ( fn.getCalcStdDev() ) ? "True" : "False", "" ) );
      item.setText( 5, Const.NVL( ( fn.getCalcMin() ) ? "True" : "False", "" ) );
      item.setText( 6, Const.NVL( ( fn.getCalcMax() ) ? "True" : "False", "" ) );
      item.setText( 7, Const.NVL( ( fn.getCalcMedian() ) ? "True" : "False", "" ) );
      double p = fn.getCalcPercentile();
      NumberFormat pF = NumberFormat.getInstance();
      pF.setMaximumFractionDigits( 2 );
      String res = ( p < 0 ) ? "" : pF.format( p * 100 );
      item.setText( 8, Const.NVL( res, "" ) );

      item.setText( 9, Const.NVL( ( fn.getInterpolatePercentile() ) ? "True" : "False", "" ) );
    }

    m_wFields.setRowNums();
    m_wFields.optWidth( true );
  }

  m_wStepname.selectAll();
  m_wStepname.setFocus();
}
 
Example 14
Source File: JTop.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setValue(Object value) {
    if (formatter==null) {
        formatter = NumberFormat.getInstance();
        formatter.setMinimumFractionDigits(4);
    }
    setText((value == null) ? "" : formatter.format(value));
}
 
Example 15
Source File: SynthTableUI.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void configureValue(Object value, Class columnClass) {
    if (columnClass == Object.class || columnClass == null) {
        setHorizontalAlignment(JLabel.LEADING);
    } else if (columnClass == Float.class || columnClass == Double.class) {
        if (numberFormat == null) {
            numberFormat = NumberFormat.getInstance();
        }
        setHorizontalAlignment(JLabel.TRAILING);
        setText((value == null) ? "" : ((NumberFormat)numberFormat).format(value));
    }
    else if (columnClass == Number.class) {
        setHorizontalAlignment(JLabel.TRAILING);
        // Super will have set value.
    }
    else if (columnClass == Icon.class || columnClass == ImageIcon.class) {
        setHorizontalAlignment(JLabel.CENTER);
        setIcon((value instanceof Icon) ? (Icon)value : null);
        setText("");
    }
    else if (columnClass == Date.class) {
        if (dateFormat == null) {
            dateFormat = DateFormat.getDateInstance();
        }
        setHorizontalAlignment(JLabel.LEADING);
        setText((value == null) ? "" : ((Format)dateFormat).format(value));
    }
    else {
        configureValue(value, columnClass.getSuperclass());
    }
}
 
Example 16
Source File: ComplexFormatAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testGetRealFormat() {
    NumberFormat nf = NumberFormat.getInstance();
    ComplexFormat cf = new ComplexFormat(nf);
    Assert.assertSame(nf, cf.getRealFormat());
}
 
Example 17
Source File: LabelGenerator.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public LabelGenerator() {
    super("", NumberFormat.getInstance());
}
 
Example 18
Source File: MatrixUtil.java    From tectonicus with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
public static float getDelta(Matrix4f left, Matrix4f right)
{
	float delta = 0;
	
	final float d00 = left.m00 - right.m00;
	final float d01 = left.m01 - right.m01;
	final float d02 = left.m02 - right.m02;
	final float d03 = left.m03 - right.m03;
	
	final float d10 = left.m10 - right.m10;
	final float d11 = left.m11 - right.m11;
	final float d12 = left.m12 - right.m12;
	final float d13 = left.m13 - right.m13;
	
	final float d20 = left.m20 - right.m20;
	final float d21 = left.m21 - right.m21;
	final float d22 = left.m22 - right.m22;
	final float d23 = left.m23 - right.m23;
	
	final float d30 = left.m30 - right.m30;
	final float d31 = left.m31 - right.m31;
	final float d32 = left.m32 - right.m32;
	final float d33 = left.m33 - right.m33;
	
	delta += d00 + d01 + d02 + d03;
	delta += d10 + d11 + d12 + d13;
	delta += d20 + d21 + d22 + d23;
	delta += d30 + d31 + d32 + d33;
	
	NumberFormat format = NumberFormat.getInstance();
	format.setMinimumFractionDigits(8);
	format.setMinimumIntegerDigits(3);
	
	System.out.println("[" + format.format(d00) + ", " + format.format(d10) + ", " + format.format(d20) + ", " + format.format(d30) + "]");
	System.out.println("[" + format.format(d01) + ", " + format.format(d11) + ", " + format.format(d21) + ", " + format.format(d31) + "]");
	System.out.println("[" + format.format(d02) + ", " + format.format(d12) + ", " + format.format(d22) + ", " + format.format(d32) + "]");
	System.out.println("[" + format.format(d03) + ", " + format.format(d13) + ", " + format.format(d23) + ", " + format.format(d33) + "]");
	System.out.println();
	
	return delta;
}
 
Example 19
Source File: StandardXYToolTipGenerator.java    From SIMVA-SoS with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a tool tip generator that formats the x-values as dates and the
 * y-values as numbers.
 *
 * @return A tool tip generator (never <code>null</code>).
 */
public static StandardXYToolTipGenerator getTimeSeriesInstance() {
    return new StandardXYToolTipGenerator(DEFAULT_TOOL_TIP_FORMAT,
            DateFormat.getInstance(), NumberFormat.getInstance());
}
 
Example 20
Source File: StandardXYToolTipGenerator.java    From buffer_bci with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Returns a tool tip generator that formats the x-values as dates and the
 * y-values as numbers.
 *
 * @return A tool tip generator (never <code>null</code>).
 */
public static StandardXYToolTipGenerator getTimeSeriesInstance() {
    return new StandardXYToolTipGenerator(DEFAULT_TOOL_TIP_FORMAT,
            DateFormat.getInstance(), NumberFormat.getInstance());
}