下面列出了java.text.DecimalFormat#setNegativePrefix ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
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);
}
public FloatingPointFormat( int w, int d ) {
width = w;
decimal = d;
nf = new DecimalFormat( pattern(w, d) );
nf.setPositivePrefix(" ");
nf.setNegativePrefix("-");
}
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("-");
}
/**
* 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("-");
}
/**
** 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("-");
}
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());
}
public Chart2ValueFormatter() {
mFormat = new DecimalFormat("###,###,##0.0"); // use one decimal
mFormat.setNegativePrefix("");
}
public void test_getNegativePrefix() {
DecimalFormat df = new DecimalFormat();
df.setNegativePrefix("--");
assertTrue("Incorrect negative prefix", df.getNegativePrefix().equals("--"));
}