Java Code Examples for java.text.DecimalFormat.setPositivePrefix()

The following are Jave code examples for showing how to use setPositivePrefix() of the java.text.DecimalFormat class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: openNaEF   File: SetTargetTimeCommand.java   Source Code and License Vote up 6 votes
private Long asOffset(String offsetValueStr, String offsetUnitStr)
        throws ShellCommandException {
    int offsetValue;
    try {
        DecimalFormat formatter = new DecimalFormat();
        formatter.setPositivePrefix("+");
        formatter.setNegativePrefix("-");
        offsetValue = formatter.parse(offsetValueStr).intValue();
    } catch (ParseException pe) {
        throw new ShellCommandException("offset value format error: " + offsetValueStr);
    }

    OffsetUnit offsetUnit;
    try {
        offsetUnit = OffsetUnit.valueOf(offsetUnitStr.toUpperCase());
    } catch (IllegalArgumentException iae) {
        throw new ShellCommandException("unsupported offset unit: " + offsetUnitStr);
    }

    Calendar calendar = new GregorianCalendar();
    calendar.setTime(new Date(getSession().getTargetTime()));
    calendar.add(offsetUnit.field, offsetValue);

    return new Long(calendar.getTime().getTime());
}
 
Example 2
Project: datarouter   File: NumberFormatter.java   Source Code and License Vote up 5 votes
public static String format(Number number, String prefix, String suffix, int numFractionDigits, boolean grouping){
	DecimalFormat df = new DecimalFormat();
	df.setMaximumFractionDigits(numFractionDigits);
	df.setMinimumFractionDigits(numFractionDigits);
	df.setRoundingMode(RoundingMode.HALF_UP);
	df.setGroupingUsed(grouping);
	df.setPositivePrefix(prefix);
	df.setNegativePrefix(prefix + "-");
	df.setPositiveSuffix(suffix);
	df.setNegativeSuffix(suffix);
	return df.format(number);
}