Example usage for java.text NumberFormat setMaximumFractionDigits

List of usage examples for java.text NumberFormat setMaximumFractionDigits

Introduction

In this page you can find the example usage for java.text NumberFormat setMaximumFractionDigits.

Prototype

public void setMaximumFractionDigits(int newValue) 

Source Link

Document

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

Usage

From source file:org.apache.hadoop.hive.ql.udf.UDFNumberFormat.java

public String evaluate(DoubleWritable d, IntWritable fracMax, IntWritable intMax) {
    if (d == null || fracMax == null || intMax == null) {
        return null;
    }//  ww w  . j a  v a2 s.c o  m
    double ori = d.get();
    try {
        NumberFormat nFormat = NumberFormat.getNumberInstance();
        nFormat.setMaximumFractionDigits(fracMax.get());
        nFormat.setMaximumIntegerDigits(intMax.get());
        nFormat.setGroupingUsed(false);
        return nFormat.format(ori);
    } catch (Exception e) {
        LOG.error("can not format value:  " + ori);
        return null;
    }
}

From source file:ClubSkeleton.java

public String callMethod(String request) {
    JSONObject result = new JSONObject();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    try {//from   ww  w.j a  v a 2  s.  c  o  m
        JSONObject theCall = new JSONObject(request);
        String method = theCall.getString("method");
        int id = theCall.getInt("id");
        JSONArray params = null;
        if (!theCall.isNull("params")) {
            params = theCall.getJSONArray("params");
        }
        result.put("id", id);
        result.put("jsonrpc", "2.0");
        double left = 0;
        double right = 0;
        if (params.length() > 0) {
            left = params.getDouble(0);
        }
        if (params.length() > 1) {
            right = params.getDouble(1);
        }
        if (method.equals("getAboutUs")) {
            //code to return the About Us page
            System.out.println("request: " + nf.format(left) + " + " + nf.format(right));
            String res = ci.getAboutUs();
            result.put("result", res);
        } else if (method.equals("getEvents")) {
            //code to return the About Us page
            System.out.println("request: " + nf.format(left) + " + " + nf.format(right));
            String res = ci.getEvents();
            result.put("result", res);
        } else if (method.equals("getNews")) {
            //code to return the About Us page
            System.out.println("request: " + nf.format(left) + " + " + nf.format(right));
            String res = ci.getNews();
            result.put("result", res);
        } else if (method.equals("getFAQ")) {
            //code to return the About Us page
            System.out.println("request: " + nf.format(left) + " + " + nf.format(right));
            String res = ci.getFAQ();
            result.put("result", res);
        } else if (method.equals("whoAreYou")) {
            System.out.println("request: whoAreYou");
            result.put("result", ci.whoAreYou());
        }
    } catch (Exception ex) {
        System.out.println("exception in callMethod: " + ex.getMessage());
    }
    return "HTTP/1.0 200 Data follows\nServer:localhost:8080\nContent-Type:text/plain\nContent-Length:"
            + (result.toString()).length() + "\n\n" + result.toString();
}

From source file:au.org.emii.geoserver.wfs.response.RestrictedColumnCsvOutputFormat.java

private NumberFormat getCoordFormatter() {
    NumberFormat coordFormatter = NumberFormat.getInstance(Locale.US);
    coordFormatter.setMaximumFractionDigits(getInfo().getGeoServer().getSettings().getNumDecimals());
    coordFormatter.setGroupingUsed(false);

    return coordFormatter;
}

From source file:org.apache.hadoop.hive.ql.udf.UDFNumberFormat.java

public String evaluate(DoubleWritable d, IntWritable fracMax, IntWritable fracMin, IntWritable intMax,
        IntWritable intMin) {/*from w  w  w. ja  v a  2s  .  co  m*/
    if (d == null || fracMax == null || intMax == null) {
        return null;
    }
    if (fracMin == null || intMin == null) {
        return null;
    }
    double ori = d.get();
    try {
        NumberFormat nFormat = NumberFormat.getNumberInstance();
        nFormat.setMaximumFractionDigits(fracMax.get());
        nFormat.setMaximumIntegerDigits(intMax.get());
        nFormat.setMinimumFractionDigits(fracMin.get());
        nFormat.setMinimumIntegerDigits(intMin.get());
        nFormat.setGroupingUsed(false);
        return nFormat.format(ori);
    } catch (Exception e) {
        LOG.error("can not format value:  " + ori);
        return null;
    }
}

From source file:org.apache.hadoop.hive.ql.udf.UDFNumberFormat.java

public String evaluate(LongWritable d, IntWritable fracMax, IntWritable fracMin, IntWritable intMax,
        IntWritable intMin) {/*from w w w  .  java2s.co  m*/
    if (d == null || fracMax == null || intMax == null) {
        return null;
    }
    if (fracMin == null || intMin == null) {
        return null;
    }
    long ori = d.get();
    try {
        NumberFormat nFormat = NumberFormat.getNumberInstance();
        nFormat.setMaximumFractionDigits(fracMax.get());
        nFormat.setMaximumIntegerDigits(intMax.get());
        nFormat.setMinimumFractionDigits(fracMin.get());
        nFormat.setMinimumIntegerDigits(intMin.get());
        nFormat.setGroupingUsed(false);
        return nFormat.format(ori);
    } catch (Exception e) {
        LOG.error("can not format value:  " + ori);
        return null;
    }
}

From source file:com.btobits.automator.ant.utils.task.RandomTask.java

@Override
public void execute() throws BuildException {
    if (min == null || min.equals(""))
        throw new BuildException("Min property not specified");

    if (max == null || max.equals(""))
        throw new BuildException("Max property not specified");

    if (type.equalsIgnoreCase("int") || StringUtils.isBlank(type)) {

        int minInt = Integer.parseInt(min);
        int maxInt = Integer.parseInt(max);

        if (minInt > maxInt)
            throw new BuildException("Min is bigger than max");

        int randomInt = calculateRandom(minInt, maxInt);
        getProject().setNewProperty(property, String.valueOf(randomInt));
    } else {//from  w  w w  . ja va 2 s  . c  o m

        double minD = Double.parseDouble(min);
        double maxD = Double.parseDouble(max);

        if (minD > maxD)
            throw new BuildException("Min is bigger than max");

        double randomDouble = calculateRandom2(minD, maxD);
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(fraction);
        getProject().setNewProperty(property, nf.format(randomDouble));
    }

    try {
        Thread.sleep(1);
    } catch (InterruptedException e) {
    }
}

From source file:org.netxilia.api.impl.format.CurrencyFormatter.java

@Override
protected NumberFormat buildFormat() {
    // TODO find a way to reuse formatters
    NumberFormat format = NumberFormat.getCurrencyInstance(getLocaleObject());
    if (decimalCount >= 0) {
        format.setMaximumFractionDigits(decimalCount);
    }/* w w  w  . ja va2 s  .com*/
    // format.setCurrency(Currency.getInstance(getPattern().getPattern()));
    return format;
}

From source file:de.tor.tribes.util.bb.BasicFormatter.java

public NumberFormat getNumberFormatter(int pDigits) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(0);
    f.setMinimumFractionDigits(0);/*from  w  ww  .ja  v  a2 s .co m*/
    if (pDigits < 10) {
        f.setMaximumIntegerDigits(1);
    } else if (pDigits < 100) {
        f.setMaximumIntegerDigits(2);
    } else if (pDigits < 1000) {
        f.setMaximumIntegerDigits(3);
    } else if (pDigits < 10000) {
        f.setMaximumIntegerDigits(4);
    } else {
        f.setMaximumIntegerDigits(5);
    }
    return f;
}

From source file:org.oscarehr.common.model.ReportStatistic.java

public String getPercent() {
    NumberFormat percentInstance = NumberFormat.getPercentInstance();
    percentInstance.setMinimumFractionDigits(0);
    percentInstance.setMaximumFractionDigits(2);

    return percentInstance.format(fraction.floatValue());
}

From source file:com.ibk.ltw.domain.Product.java

public String getVatAsText() {
    BigDecimal vatInPercent = new BigDecimal(getVatInPerThousand()).movePointLeft(3);
    NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.US);
    percentFormat.setMaximumFractionDigits(1);
    percentFormat.setRoundingMode(RoundingMode.HALF_DOWN);
    return percentFormat.format(vatInPercent);
}