Java Code Examples for java.text.DecimalFormat#setNegativePrefix()

The following examples show how to use java.text.DecimalFormat#setNegativePrefix() . These examples are extracted from open source projects. 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 Project: sailfish-core   File: GenericConverter.java    License: Apache License 2.0 6 votes vote down vote up
public static <T extends Number> byte[] convertSignedNumericToArray(int length, T value, int precision )
{

       // Pattern for fractional part
	char[] arrFractional = new char[precision];
	Arrays.fill(arrFractional, '0');

	// Pattern for mantissa
	char[] arrMantissa = new char[length - 1 - precision ];
	Arrays.fill(arrMantissa, '0');

	String patternDesired = String.format( "%s.%s", new String(arrMantissa), new String(arrFractional));
	DecimalFormat formatter = new DecimalFormat(patternDesired );
	formatter.setPositivePrefix("+");
	formatter.setNegativePrefix("-");
	formatter.setGroupingUsed(false);
	String formattedValue = formatter.format(value);
	DecimalFormatSymbols decimalFormatSymbol = formatter.getDecimalFormatSymbols();
	formattedValue = formattedValue.replace(String.valueOf(decimalFormatSymbol.getDecimalSeparator()), "");
       return formattedValue.getBytes(charsetISO_8859);
}
 
Example 2
Source Project: sailfish-core   File: GenericConverter.java    License: Apache License 2.0 6 votes vote down vote up
public static <T extends Number> byte[] convertUnsignedNumericToArray(int length, T value, int precision )
{

       // Pattern for fractional part
	char[] arrFractional = new char[precision];
	Arrays.fill(arrFractional, '0');

	// Pattern for mantissa
	char[] arrMantissa = new char[length - precision ];
	Arrays.fill(arrMantissa, '0');

	String patternDesired = String.format( "%s.%s", new String(arrMantissa), new String(arrFractional));
	DecimalFormat formatter = new DecimalFormat(patternDesired );
	formatter.setPositivePrefix("");
	formatter.setNegativePrefix("");
	formatter.setGroupingUsed(false);
	String formattedValue = formatter.format(value);
	DecimalFormatSymbols decimalFormatSymbol = formatter.getDecimalFormatSymbols();
	formattedValue = formattedValue.replace(String.valueOf(decimalFormatSymbol.getDecimalSeparator()), "");
       return formattedValue.getBytes(charsetISO_8859);
}
 
Example 3
public FloatingPointFormat( int w, int d ) {
  width = w;
  decimal = d;
  nf = new DecimalFormat( pattern(w, d) );
  nf.setPositivePrefix(" ");
  nf.setNegativePrefix("-");
}
 
Example 4
public ExponentialFormat( int digits, int exp, boolean sign, 
	    boolean trailing ) {
  this.digits = digits;
  this.exp = exp;
  this.sign = sign;
  this.trailing = trailing;
  nf = new DecimalFormat( pattern() );
  nf.setPositivePrefix("+");
  nf.setNegativePrefix("-");
}
 
Example 5
/**
   * Constructor. Creates a FloatingPointFormat with the given values width and decimal size.
   * @param w width given.
   * @param d decimal size given.
   */
  public FloatingPointFormat( int w, int d ) {
  width = w;
  decimal = d;
  nf = new DecimalFormat( pattern(w, d) );
  nf.setPositivePrefix(" ");
  nf.setNegativePrefix("-");
}
 
Example 6
/**
   ** Constructor. Creates a ExponentialFormat with the given arguments.
   * @param digits given base size.
   * @param exp given exponent size.
   * @param sign given sign flag.
   * @param trailing given trailing flag.
   */
  public ExponentialFormat( int digits, int exp, boolean sign, 
	    boolean trailing ) {
  this.digits = digits;
  this.exp = exp;
  this.sign = sign;
  this.trailing = trailing;
  nf = new DecimalFormat( pattern() );
  nf.setPositivePrefix("+");
  nf.setNegativePrefix("-");
}
 
Example 7
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 5 votes vote down vote up
public void test_setNegativePrefix() throws Exception {
    DecimalFormat format = new DecimalFormat();
    assertEquals("-", format.getNegativePrefix());

    format.setNegativePrefix("NegPrf");
    assertEquals("NegPrf", format.getNegativePrefix());
    assertTrue(format.parse("NegPrf123.45").doubleValue() == -123.45);
    format.setNegativePrefix("");
    assertEquals("", format.getNegativePrefix());

    format.setNegativePrefix(null);
    assertNull(format.getNegativePrefix());
}
 
Example 8
public Chart2ValueFormatter() {
    mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
    mFormat.setNegativePrefix("");
}
 
Example 9
Source Project: j2objc   File: DecimalFormatTest.java    License: Apache License 2.0 4 votes vote down vote up
public void test_getNegativePrefix() {
    DecimalFormat df = new DecimalFormat();
    df.setNegativePrefix("--");
    assertTrue("Incorrect negative prefix", df.getNegativePrefix().equals("--"));
}