Example usage for java.text NumberFormat format

List of usage examples for java.text NumberFormat format

Introduction

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

Prototype

public final String format(long number) 

Source Link

Document

Specialization of format.

Usage

From source file:com.kingcore.framework.util.ConvertUtils.java

/**
 * ??//  w ww.  jav  a  2  s.  com
 * @param String ?
 * @param int ???
 * @param boolean ?
 * @param int ???
 * @return String
 */
public static String formatMoney(String money, int scalar, boolean isround, int standard) throws Exception {
    String formatMoney = null;
    if (isround)
        formatMoney = round(money, scalar, standard);
    String zero = "000000000000000000000000000000";
    String format = "###,##0." + zero.substring(0, scalar);
    java.text.NumberFormat nf = new java.text.DecimalFormat(format);
    return (nf.format(formatMoney));
}

From source file:com.kingcore.framework.util.ConvertUtils.java

/**
 * ??/*from  ww w  .j a va2s  .c  o  m*/
 * @param double ?
 * @param int ???
 * @param boolean ?
 * @param int ???
 * @return String
 */
public static String formatMoney(double money, int scalar, boolean isround, int standard) throws Exception {
    String formatMoney = null;
    if (isround)
        formatMoney = round(String.valueOf(money), scalar, standard);
    String zero = "000000000000000000000000000000";
    String format = "###,##0." + zero.substring(0, scalar);
    java.text.NumberFormat nf = new java.text.DecimalFormat(format);
    return (nf.format(formatMoney));
}

From source file:dataGen.DataGen.java

/**
 * Save the Date with the FileWriter/*from  www.  ja va 2s .c  o  m*/
 * @param values
 * @param fw
 * @param nf
 * @throws IOException
 */
public static void saveData(TupleList values, Writer fw, NumberFormat nf) throws IOException {
    for (int i = 0; i < values.size(); i++) {
        String row = i + 1 + " ";
        //String row = "";

        for (int j = 0; j < values.get(i).size(); j++) {
            row = row + nf.format(values.get(i).getValue(j)) + " ";
        }
        fw.write(row);
        fw.append(System.getProperty("line.separator"));
    }
    fw.close();
}

From source file:Main.java

public static String getBalanceDisplay(long amount, int floatSize) {
    NumberFormat format = NumberFormat.getInstance();
    if (NumEmpty == amount)
        return "";
    if (floatSize < 0) { // error input
        return Long.toString(amount);
    }/*w ww .  j av  a 2  s  . c  o  m*/
    if (floatSize == 2) {
        boolean zs = true;
        if (amount < 0) {
            zs = false;
            amount = -amount;
        }
        long m = amount / 100;
        long f = amount - m * 100;
        return (zs ? "" : "-") + format.format(m) + "." + (f > 9 ? Long.toString(f) : "0" + f);
    } else if (floatSize == 0) {
        return format.format(amount);
    } else { // error input
        return format.format(amount);
    }
}

From source file:dataGen.DataGen.java

/**
 * Saves the Data/*  w ww . jav  a  2 s.co m*/
 * @param values
 *       The values which should be saved.
 * @param fw
 *       The File-Writer including the File location
 * @param nf
 *       How should the data get formatted?
 * @throws IOException
 *       If Stream to a File couldn't be written/closed 
 */
public static void saveData(Array2DRowRealMatrix values, Writer fw, NumberFormat nf) throws IOException {
    for (int i = 0; i < values.getRowDimension(); i++) {
        String row = i + 1 + " ";
        //String row = "";

        for (int j = 0; j < values.getColumnDimension(); j++) {
            row = row + nf.format(values.getEntry(i, j)) + " ";
        }
        fw.write(row);
        fw.append(System.getProperty("line.separator"));
    }
    fw.close();
}

From source file:com.edgenius.core.util.FileUtil.java

/**
 * Check whether file is executable according to its extenstion and executable extension name list from Edgenius configuaration.
 * @param filename//from  ww w .  ja va  2 s. c  o  m
 * @return
 */
//     public static boolean isExecutableFile(String filename){
//        String extname = FileUtil.getFileExtension(filename);
//        log.debug("Check executable file for extension name " + extname);
//        
//        if(StringUtils.isBlank(extname))
//           return false;
//        extname = "." + extname;
//        
//        String exeListStr = Global.ExecuteFileExt;
//        String[] extList = StringUtils.split(exeListStr, ',');
//        boolean executable = false;
//        for (String ext : extList) {
//         if(StringUtils.equalsIgnoreCase(ext, extname)){
//            executable = true;
//            break;
//         }
//      }
//        
//        return executable;
//     }

public static String convertHumanSize(long byteSize) {
    String unit = "";
    float size = (float) byteSize;
    if (size > 1024) {
        size = size / 1024;
        unit = "K";
    }
    if (size > 1024) {
        size = size / 1024;
        unit = "M";
    }
    if (size > 1024) {
        size = size / 1024;
        unit = "G";
    }

    NumberFormat format = NumberFormat.getNumberInstance();
    format.setMaximumFractionDigits(1);
    String ret = format.format(size);

    return ret + unit;
}

From source file:Main.java

public static String getBalanceDisplay(String bal, int floatSize) {
    if ("".equals(bal)) {
        return "";
    } else {//from  w w w . j  a va  2s  . c o  m
        long amount = Long.parseLong(bal);
        NumberFormat format = NumberFormat.getInstance();
        if (NumEmpty == amount)
            return "";
        if (floatSize < 0) { // error input
            return Long.toString(amount);
        }
        if (floatSize == 2) {
            boolean zs = true;
            if (amount < 0) {
                zs = false;
                amount = -amount;
            }
            long m = amount / 100;
            long f = amount - m * 100;
            return (zs ? "" : "-") + format.format(m) + "." + (f > 9 ? Long.toString(f) : "0" + f);
        } else if (floatSize == 0) {
            return format.format(amount);
        } else { // error input
            return format.format(amount);
        }
    }
}

From source file:edu.kit.dama.rest.util.RestClientUtils.java

/**
 * Build REST URL from a given pattern and its values. There is no check for
 * correct number of arguments. While generating URL all arguments will
 * encoded to be in a correct format. E.g.: 'stupid example' will be
 * transformed to 'stupid%20example'./*from  ww  w . j av a 2s  .  c  o  m*/
 *
 * @param pattern URL with any number of place holders for arguments
 * @param arguments array of arguments for the place holders
 * @return encoded URL
 */
public static String encodeUrl(String pattern, Object... arguments) {
    List<Object> urlArg = new ArrayList<>();
    for (Object arg : arguments) {
        if (arg == null) {
            throw new IllegalArgumentException("Null-values not supported for any element of 'arguments'.");
        }
        if (arg instanceof Number) {
            // everyting should work fine!
            NumberFormat instance = NumberFormat.getInstance(Locale.US);
            instance.setGroupingUsed(false);
            instance.setMaximumFractionDigits(9);
            String number = instance.format(arg);
            urlArg.add(number);
        } else if (arg instanceof String) {
            urlArg.add(encode((String) arg));
        } else {
            LOGGER.warn("Uncovered argument type {}", arg);
        }
    }
    String returnValue = MessageFormat.format(pattern, urlArg.toArray());

    LOGGER.debug("Final URL: {}", returnValue);

    return returnValue;
}

From source file:com.scf.core.EnvTest.java

public static String formatMoney(BigDecimal money, int len) {
    NumberFormat formater = null;
    if (len == 0) {
        formater = new DecimalFormat("###,###");
    } else {// w  ww  .ja v a 2  s .co  m
        StringBuffer buff = new StringBuffer();
        buff.append("###,##0.");
        for (int i = 0; i < len; i++) {
            buff.append("0");
        }
        formater = new DecimalFormat(buff.toString());
        formater.setRoundingMode(RoundingMode.HALF_UP);
    }
    return formater.format(money);
}

From source file:mt.LengthDistribution.java

public static void GetLengthDistributionArray(ArrayList<File> AllMovies, double[] calibration) {

    ArrayList<Double> maxlist = new ArrayList<Double>();
    for (int i = 0; i < AllMovies.size(); ++i) {

        double maxlength = LengthDistribution.Lengthdistro(AllMovies.get(i));

        if (maxlength != Double.NaN && maxlength > 0)
            maxlist.add(maxlength);/*from w ww.  j a v a  2s.c o m*/

    }
    Collections.sort(maxlist);

    int min = 0;
    int max = (int) Math.round(maxlist.get(maxlist.size() - 1)) + 1;
    XYSeries counterseries = new XYSeries("MT length distribution");
    XYSeries Logcounterseries = new XYSeries("MT Log length distribution");
    final ArrayList<Point> points = new ArrayList<Point>();
    for (int length = 0; length < max; ++length) {

        HashMap<Integer, Integer> frameseed = new HashMap<Integer, Integer>();

        int count = 0;
        for (int i = 0; i < AllMovies.size(); ++i) {

            File file = AllMovies.get(i);

            double currentlength = LengthDistribution.Lengthdistro(file);

            ArrayList<FLSobject> currentobject = Tracking.loadMTStat(file);

            if (currentlength > length) {

                for (int index = 0; index < currentobject.size(); ++index) {
                    ArrayList<Integer> seedlist = new ArrayList<Integer>();
                    if (currentobject.get(index).length >= length) {
                        seedlist.add(currentobject.get(index).seedID);
                        if (frameseed.get(currentobject.get(index).Framenumber) != null
                                && frameseed.get(currentobject.get(index).Framenumber) != Double.NaN) {

                            int currentcount = frameseed.get(currentobject.get(index).Framenumber);
                            frameseed.put(currentobject.get(index).Framenumber, seedlist.size() + currentcount);
                        } else if (currentobject.get(index) != null)
                            frameseed.put(currentobject.get(index).Framenumber, seedlist.size());

                    }

                }

            }

        }

        // Get maxima length, count
        int maxvalue = Integer.MIN_VALUE;

        for (int key : frameseed.keySet()) {

            int Count = frameseed.get(key);

            if (Count >= maxvalue)
                maxvalue = Count;
        }

        if (maxvalue != Integer.MIN_VALUE) {
            counterseries.add(length, maxvalue);

            if (maxvalue > 0) {
                Logcounterseries.add((length), Math.log(maxvalue));
                points.add(new Point(new double[] { length, Math.log(maxvalue) }));
            }

        }
    }

    final XYSeriesCollection dataset = new XYSeriesCollection();
    final XYSeriesCollection nofitdataset = new XYSeriesCollection();
    dataset.addSeries(counterseries);
    nofitdataset.addSeries(counterseries);
    final XYSeriesCollection Logdataset = new XYSeriesCollection();
    Logdataset.addSeries(Logcounterseries);

    final JFreeChart chart = ChartFactory.createScatterPlot("MT length distribution", "Number of MT",
            "Length (micrometer)", dataset);

    final JFreeChart nofitchart = ChartFactory.createScatterPlot("MT length distribution", "Number of MT",
            "Length (micrometer)", nofitdataset);

    // Fitting line to log of the length distribution
    interpolation.Polynomial poly = new interpolation.Polynomial(1);
    try {

        poly.fitFunction(points);

    } catch (NotEnoughDataPointsException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    DisplayPoints.display(nofitchart, new Dimension(800, 500));
    dataset.addSeries(Tracking.drawexpFunction(poly, counterseries.getMinX(), counterseries.getMaxX(), 0.5,
            "Exponential fit"));
    NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
    nf.setMaximumFractionDigits(3);
    TextTitle legendText = new TextTitle("Mean Length" + " : " + nf.format(-1.0 / poly.getCoefficients(1))
            + "  " + "Standard Deviation" + " : " + nf.format(poly.SSE));
    legendText.setPosition(RectangleEdge.RIGHT);

    DisplayPoints.display(chart, new Dimension(800, 500));
    chart.addSubtitle(legendText);

    final JFreeChart logchart = ChartFactory.createScatterPlot("MT Log length distribution", "Number of MT",
            "Length (micrometer)", Logdataset);
    //     DisplayPoints.display(logchart, new Dimension(800, 500));
    for (int i = 1; i >= 0; --i)
        System.out.println(poly.getCoefficients(i) + "  " + "x" + " X to the power of " + i);

    //  Logdataset.addSeries(Tracking.drawFunction(poly, counterseries.getMinX(), counterseries.getMaxX(), 0.5, "Straight line fit"));

    WriteLengthdistroFile(AllMovies, counterseries, 0);

}