Example usage for java.text DecimalFormat setMinimumFractionDigits

List of usage examples for java.text DecimalFormat setMinimumFractionDigits

Introduction

In this page you can find the example usage for java.text DecimalFormat setMinimumFractionDigits.

Prototype

@Override
public void setMinimumFractionDigits(int newValue) 

Source Link

Document

Sets the minimum number of digits allowed in the fraction portion of a number.

Usage

From source file:com.micabyte.android.app.BaseActivity.java

@SuppressWarnings({ "rawtypes", "MagicNumber" })
public static void logHeap(Class clazz) {
    final DecimalFormat df = new DecimalFormat();
    df.setMaximumFractionDigits(2);/*from   w  ww  . j  a v a  2s  .  c o  m*/
    df.setMinimumFractionDigits(2);
    if (BuildConfig.DEBUG)
        Log.d(clazz.getName(),
                "DEBUG_MEMORY allocated " + df.format((double) (Runtime.getRuntime().totalMemory() / 1048576))
                        + "/" + df.format((double) (Runtime.getRuntime().maxMemory() / 1048576)) + "MB ("
                        + df.format((double) (Runtime.getRuntime().freeMemory() / 1048576)) + "MB free)");
    System.gc();
    System.gc();
}

From source file:de.unidue.inf.is.ezdl.dlcore.utils.StringUtils.java

/**
 * Returns a double value as a string with a given number of decimal digits.
 * //from w  w  w .  ja  v a2s  .  c o  m
 * @param value
 *            double value
 * @param numberOfDecimals
 *            number of decimal digits
 * @return string with a given number of decimal digits
 */
public static String toString(double value, int numberOfDecimals) {
    DecimalFormat df = new DecimalFormat("#.#");
    df.setMinimumFractionDigits(numberOfDecimals);
    return df.format(value);
}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(BigDecimal val) {
    String s = "";
    try {/*from  w  ww  .  j av  a  2s. c om*/
        DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
        df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
        df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
        df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
        df.setRoundingMode(Preferences.ROUNDING_MODE);
        s = String.valueOf(df.format(val));
    } catch (Exception ex) {
        br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***", ex.getMessage());
    }
    return s;

}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(float val) {
    String s = "";
    if (val > 0) {
        try {/*from ww  w .j a v a2 s  .  co m*/
            DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
            df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
            df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
            df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
            df.setRoundingMode(Preferences.ROUNDING_MODE);
            s = String.valueOf(df.format(val));
        } catch (Exception ex) {
            br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***",
                    ex.getMessage());
        }
    }
    return s;

}

From source file:br.com.itfox.utils.Utils.java

public static String formatDecimal(double val) {
    String s = "";
    if (val > 0) {
        try {/*from  w  ww. j av  a  2 s. co  m*/
            DecimalFormat df = (DecimalFormat) DecimalFormat.getInstance(new Locale("en", "EN"));
            df.setMaximumFractionDigits(Preferences.MAXIMUM_FRACTION_DIGITS);
            df.setMinimumFractionDigits(Preferences.MINIMUM_FRACTION_DIGITS);
            df.setMinimumIntegerDigits(Preferences.MIMIMUM_INTEGER_DIGITS);
            df.setRoundingMode(Preferences.ROUNDING_MODE);
            s = String.valueOf(df.format(val));
        } catch (Exception ex) {
            br.com.itfox.utils.Logger.getLogger(ex, Utils.class.getName() + "***" + val + "***",
                    ex.getMessage());
        }
    }
    return s;

}

From source file:org.renjin.base.StrSignIf.java

private static NumberFormat buildFormat(int digits, String format, String flag) {
    if (format.equals("d")) {
        return NumberFormat.getIntegerInstance();
    } else {/*from  w  ww . j  a  v  a 2s . c  om*/

        DecimalFormat formatter;
        if (format.equals("e")) {
            formatter = new DecimalFormat("0.00e0");
        } else if (format.equals("E")) {
            formatter = new DecimalFormat("0.00E0");
        } else {
            formatter = new DecimalFormat();
        }
        if (digits < 0) {
            digits = 6;
        }
        formatter.setMaximumFractionDigits(digits);
        formatter.setMinimumFractionDigits(digits);
        return formatter;
    }
}

From source file:com.cssweb.quote.util.Utils.java

/**
 * ??/*from  ww  w. j  a  va2s . com*/
 * @param d
 * @param t
 * @return
 */
public static String dataFormation(double d, int t) {
    DecimalFormat df = null;
    switch (t) {
    case 0:
        df = new DecimalFormat("#.##");
        df.setMinimumFractionDigits(0);
        return df.format(d);
    case 1:
        df = new DecimalFormat("#.##");
        df.setMinimumFractionDigits(2);
        return df.format(d);
    case 2:
        df = new DecimalFormat("#.###");
        df.setMinimumFractionDigits(3);
        return df.format(d);
    case 3:
        df = new DecimalFormat("#.####");
        df.setMinimumFractionDigits(4);
        return df.format(d);
    case 4:
        df = new DecimalFormat("#.###%");
        df.setMinimumFractionDigits(3);
        return df.format(d / 100);
    case 5:
        return String.valueOf((int) d);
    case 6:
        df = new DecimalFormat("#.##%");
        df.setMinimumFractionDigits(2);
        return df.format(d);
    case 7:
        df = new DecimalFormat("#.#");
        df.setMinimumFractionDigits(1);
        return df.format(d);
    case 8:
        df = new DecimalFormat("#.##%");
        df.setMinimumFractionDigits(2);
        return df.format(d / 100);
    default:
        return "0";

    }
}

From source file:com.itude.mobile.mobbl.core.util.StringUtilities.java

private static void setupFormatter(DecimalFormat formatter, int minumumDecimals, int maximumDecimals) {
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(getDefaultFormattingLocale()));
    formatter.setMinimumIntegerDigits(1);

    if (minumumDecimals > -1) {
        formatter.setMinimumFractionDigits(minumumDecimals);
    }/*  w w  w .  ja v  a2s.  c  o  m*/
    if (maximumDecimals > -1) {
        formatter.setMaximumFractionDigits(maximumDecimals);
    }
    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * Set the formatter.// ww  w . ja  va  2  s .  c  om
 * 
 * @param formatter {@link DecimalFormat}
 * @param locale {@link Locale}
 * @param minimalDecimalNumbers minimal number of decimals
 * @param maximumDecimalNumbers maximum number of decimals
 */
private static void setupFormatter(DecimalFormat formatter, Locale locale, int minimalDecimalNumbers,
        int maximumDecimalNumbers) {
    formatter.setDecimalFormatSymbols(new DecimalFormatSymbols(locale));
    formatter.setMinimumIntegerDigits(1);
    formatter.setMinimumFractionDigits(minimalDecimalNumbers);
    formatter.setMaximumFractionDigits(maximumDecimalNumbers);
    formatter.setGroupingUsed(true);
    formatter.setGroupingSize(3);
}

From source file:net.sf.webissues.core.WebIssuesTaskDataHandler.java

public static Set<TaskAttribute> updateTaskData(Set<TaskAttribute> changedAttributes, WebIssuesClient client,
        TaskRepository repository, TaskData data, IssueDetails issue, IProgressMonitor monitor)
        throws HttpException, ProtocolException, IOException {
    doDateAttribute(TaskAttribute.DATE_CREATION, data, changedAttributes, issue.getIssue().getCreatedDate());
    doDateAttribute(TaskAttribute.DATE_MODIFICATION, data, changedAttributes,
            issue.getIssue().getModifiedDate());

    IEnvironment environment = WebIssuesCorePlugin.getDefault().getConnector().getClientManager()
            .getClient(repository, monitor).getEnvironment();
    Folder folder = issue.getIssue().getFolder();
    IssueType type = folder.getType();/*w w w.j ava 2s  .co m*/

    // Set the project and folder attributes
    TaskAttribute projectAttribute = data.getRoot().getAttribute(WebIssuesAttribute.PROJECT.getTaskKey());
    TaskAttribute folderAttribute = data.getRoot().getAttribute(WebIssuesAttribute.FOLDER.getTaskKey());
    projectAttribute.setValue(String.valueOf(folder.getProject().getId()));
    rebuildFolders(environment, projectAttribute, data, folderAttribute,
            issue.getIssue().getFolder().getType());
    folderAttribute.setValue(String.valueOf(folder.getId()));

    // Remove all webissues attributes exception project and folder
    for (Iterator<TaskAttribute> attributeIterator = data.getRoot().getAttributes().values()
            .iterator(); attributeIterator.hasNext();) {
        TaskAttribute attr = attributeIterator.next();
        if (attr.getId().startsWith(WEBISSUES_ATTRIBUTE_KEY_PREFIX)) {
            attributeIterator.remove();
        }
    }

    // We might already have a priority attribute
    TaskAttribute priorityAttribute = data.getRoot().getAttribute(TaskAttribute.PRIORITY);

    for (Attribute attr : type.values()) {
        if (!attr.isBuiltIn() && (priorityAttribute == null || !attr.getName().equals("Priority"))) {
            String defaultValue = attr.getDefaultValue();
            String attributeId = WEBISSUES_ATTRIBUTE_KEY_PREFIX + attr.getId();

            // Set as default if nothing set
            String value = issue.getIssue().get(attr);
            if (Util.isNullOrBlank(value)) {
                value = defaultValue;
            }

            // Set up the task attribute
            TaskAttribute taskAttr = data.getRoot().createAttribute(attributeId);
            TaskAttributeMetaData metaData = taskAttr.getMetaData();
            metaData.setKind(TaskAttribute.KIND_DEFAULT);
            switch (attr.getAttributeType()) {
            case DATETIME:
                if (attr.isDateOnly()) {
                    metaData.setType(TaskAttribute.TYPE_DATE);
                } else {
                    metaData.setType(TaskAttribute.TYPE_DATETIME);
                }
                break;
            case TEXT:
                metaData.setType(TaskAttribute.TYPE_SHORT_TEXT);
                break;
            case NUMERIC:
                metaData.setType(TaskAttribute.TYPE_SHORT_TEXT);
                break;
            case USER:
                metaData.setType(TaskAttribute.TYPE_PERSON);
                metaData.setKind(TaskAttribute.KIND_PEOPLE);
                metaData.putValue("membersOnly", String.valueOf(attr.isMembersOnly()));
                if ("[Me]".equals(value)) {
                    value = type.getTypes().getEnvironment().getOwnerUser().getName();
                }
                break;
            case ENUM:
                metaData.setType(TaskAttribute.TYPE_SINGLE_SELECT);
                taskAttr.clearOptions();
                if (!attr.isRequired()) {
                    taskAttr.putOption("", "");
                }
                for (String option : attr.getOptions()) {
                    taskAttr.putOption(option, option);
                }
                break;
            }

            // Format the value if it is numeric
            if (attr.getType().equals(Attribute.AttributeType.NUMERIC) && attr.getDecimalPlaces() > 0) {
                DecimalFormat fmt = new DecimalFormat();
                fmt.setMinimumFractionDigits(attr.getDecimalPlaces());
                fmt.setMaximumFractionDigits(attr.getDecimalPlaces());
                taskAttr.setValue(fmt.format(Util.isNullOrBlank(value) ? 0d : Double.parseDouble(value)));
            } else if (attr.getType().equals(Attribute.AttributeType.DATETIME)) {
                DateFormat fmt = new SimpleDateFormat(
                        attr.isDateOnly() ? Client.DATEONLY_FORMAT : Client.DATETIME_FORMAT);
                try {
                    taskAttr.setValue(value == null ? "" : String.valueOf(fmt.parse(value).getTime()));
                } catch (ParseException e) {
                    taskAttr.setValue("");
                }
            } else {
                taskAttr.setValue(Util.nonNull(value));
            }

            metaData.setLabel(attr.getName());
        }
    }

    // Comments
    Collection<Comment> comments = issue.getComments();
    if (comments != null) {
        int count = 1;
        for (Comment comment : comments) {
            String plainText = comment.getText();
            if (Util.isNullOrBlank(plainText)) {
                continue;
            }
            TaskCommentMapper mapper = new TaskCommentMapper();
            User owner = comment.getCreatedUser();
            if (owner != null) {
                mapper.setAuthor(repository.createPerson(owner.getName()));
            }
            mapper.setCreationDate(comment.getCreatedDate().getTime());
            mapper.setText(plainText);
            mapper.setNumber(count);
            TaskAttribute attribute = data.getRoot().createAttribute(TaskAttribute.PREFIX_COMMENT + count);
            mapper.applyTo(attribute);
            count++;
        }
    }

    Collection<Attachment> attachments = issue.getAttachments();
    int count = 1;
    for (Attachment attachment : attachments) {
        TaskAttachmentMapper mapper = new TaskAttachmentMapper();
        mapper.setAuthor(repository.createPerson(attachment.getCreatedUser().getName()));
        mapper.setDescription(attachment.getDescription());
        mapper.setCreationDate(attachment.getCreatedDate().getTime());
        mapper.setAttachmentId(String.valueOf(attachment.getId()));
        mapper.setFileName(attachment.getName());
        mapper.setUrl(Util.concatenateUri(repository.getRepositoryUrl(), "attachments",
                String.valueOf(attachment.getId())));
        mapper.setLength(Long.valueOf(attachment.getSize()));
        TaskAttribute attribute = data.getRoot().createAttribute(TaskAttribute.PREFIX_ATTACHMENT + count);
        mapper.applyTo(attribute);
        count++;
    }

    count = 1;
    for (Change change : issue.getChanges()) {
        WebIssuesChangeMapper mapper = new WebIssuesChangeMapper();
        mapper.setChangeId(String.valueOf(count));
        mapper.setUser(repository.createPerson(change.getModifiedUser().getName()));
        mapper.setAttributeName(change.getAttribute() == null ? "Name" : change.getAttribute().getName());
        mapper.setType(change.getType());
        mapper.setOldValue(change.getOldValue());
        mapper.setNewValue(change.getNewValue());
        mapper.setDate(change.getModifiedDate());
        TaskAttribute attribute = data.getRoot()
                .createAttribute(WebIssuesChangeMapper.PREFIX_CHANGE + String.valueOf(count));
        mapper.applyTo(attribute);
        count++;
    }

    return changedAttributes;
}