下面列出了java.text.DecimalFormat#setPositivePrefix ( ) 实例代码,或者点击链接到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("-");
}
/**
* Return a matrix as a String.
*
* @return
*/
@Override
public synchronized String toString() {
if ((rows <= 0) || (columns <= 0)) {
return "[]";
}
int i, j;
DecimalFormat format = new DecimalFormat("00.0000");
format.setPositivePrefix("+");
StringBuilder str = new StringBuilder();
for (i = 0; i < (rows - 1); i++) {
for (j = 0; j < (columns - 1); j++) {
if (round(matrix[i][j] * 10000) != 0) {
str.append(format.format(matrix[i][j])).append(" ");
} else {
str.append("-------- ");
}
}
if (round(matrix[i][columns - 1] * 10000) != 0) {
str.append(format.format(matrix[i][columns - 1])).append(NEW_LINE);
} else {
str.append("--------").append(NEW_LINE);
}
}
for (j = 0; j < (columns - 1); j++) {
if (round(matrix[rows - 1][j] * 10000) != 0) {
str.append(format.format(matrix[rows - 1][j])).append(" ");
} else {
str.append("-------- ");
}
}
if (round(matrix[rows - 1][columns - 1] * 10000) != 0) {
str.append(format.format(matrix[rows - 1][columns - 1]));
} else {
str.append("-------- ");
}
return str.toString();
}
public void test_setPositivePrefix() throws Exception {
DecimalFormat format = new DecimalFormat();
assertEquals("", format.getPositivePrefix());
format.setPositivePrefix("PosPrf");
assertEquals("PosPrf", format.getPositivePrefix());
assertTrue(format.parse("PosPrf123.45").doubleValue() == 123.45);
format.setPositivePrefix("");
assertEquals("", format.getPositivePrefix());
format.setPositivePrefix(null);
assertNull(format.getPositivePrefix());
}
public void test_getPositivePrefix() {
DecimalFormat df = new DecimalFormat();
df.setPositivePrefix("++");
assertTrue("Incorrect positive prefix", df.getPositivePrefix().equals("++"));
}