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.tomtom.speedtools.testutils.TestUtils.java

/**
 * Return a valid Uid string.//from w  w  w.j av a2  s.  c o m
 *
 * @return Uid string.
 */
@Nonnull
public static String getUniqueUid() {

    // NumberFormat is NOT thread-safe, so it must be declared locally here (not static).
    final NumberFormat numberFormat = new DecimalFormat("000000000000");
    final String uid = "12345678-9012-3456-7890-" + numberFormat.format(lastUid.getAndIncrement());
    return uid;
}

From source file:driimerfinance.helpers.FinanceHelper.java

/**
  * Creates a formatted string from the number input.
  * /*from   w  w w.j a v a2 s .  c  o m*/
  * @param double number to convert
  * @return a formatted number to be put on display for the user
  */
public static String formatAmount(double number) {
    NumberFormat numberFormatter = NumberFormat.getCurrencyInstance();
    String formattedString = numberFormatter.format(number);
    return formattedString;
}

From source file:com.hangum.tadpole.engine.sql.util.tables.SQLResultLabelProvider.java

/**
 * ??  , ??./*from www.j  ava2  s  .  c o  m*/
 * 
 * @param value
 * @return
 */
public static String addComma(Object value) {
    if (value == null)
        return PublicTadpoleDefine.DEFINE_NULL_VALUE;

    try {
        NumberFormat nf = NumberFormat.getNumberInstance();
        return nf.format(value);
    } catch (Exception e) {
        // ignore exception
    }

    return value.toString();
}

From source file:de.tynne.benchmarksuite.Main.java

private static String format(Args args, double number) throws IOException {
    NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
    nf.setGroupingUsed(false);/*from  ww  w .j  ava2s . c  om*/
    String string = nf.format(number);
    return string.replaceAll("\\.", args.getDecimalDot());
}

From source file:com.norbl.util.StringUtil.java

public static String f1(double x) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(1);/*from w  w  w  . j  a  va2  s .c o m*/
    f.setMinimumFractionDigits(1);
    return (f.format(x));
}

From source file:com.norbl.util.StringUtil.java

public static String f2(double x) {
    NumberFormat f = NumberFormat.getInstance();
    f.setMaximumFractionDigits(2);/*from  ww w  . j a v  a 2 s.  c  o  m*/
    f.setMinimumFractionDigits(2);
    return (f.format(x));
}

From source file:Main.java

public static String sGetDecimalStringAnyLocaleAs1Pt5LocalisedString(String value) {

    Locale theLocale = Locale.getDefault();

    NumberFormat numberFormat = DecimalFormat.getInstance(theLocale);
    Number theNumber;//from   w ww .j  a va  2  s. co  m
    try {
        theNumber = numberFormat.parse(value);
    } catch (ParseException e) {

        //value = sConvertDigits(value);
        String valueWithDot = value.replaceAll(",", ".");

        theNumber = Double.valueOf(valueWithDot);
    }

    // String.format uses the JVM's default locale.
    //String s = String.format(Locale.US, "%.2f", price);
    NumberFormat outputFormat = DecimalFormat.getInstance(theLocale);
    outputFormat.setMinimumIntegerDigits(1);
    outputFormat.setMinimumFractionDigits(5);
    outputFormat.setMaximumFractionDigits(5);
    String theStringResult = outputFormat.format(theNumber);
    //String theStringResult = String.format("%1.5f", theDoubleValue.doubleValue());

    return theStringResult;
}

From source file:com.streamsets.pipeline.cluster.ClusterFunctionImpl.java

private static synchronized void initialize(Properties properties, Integer id, String rootDataDir)
        throws Exception {
    if (initialized) {
        return;/* w  ww.  j  av a2 s. c  o  m*/
    }
    File dataDir = new File(System.getProperty("user.dir"), "data");
    FileUtils.copyDirectory(new File(rootDataDir), dataDir);
    System.setProperty("sdc.data.dir", dataDir.getAbsolutePath());
    // must occur before creating the EmbeddedSDCPool as
    // the hdfs target validation evaluates the sdc:id EL
    NumberFormat numberFormat = NumberFormat.getInstance();
    numberFormat.setMinimumIntegerDigits(6);
    numberFormat.setGroupingUsed(false);
    final String sdcId = numberFormat.format(id);
    Utils.setSdcIdCallable(new Callable<String>() {
        @Override
        public String call() {
            return sdcId;
        }
    });
    sdcPool = new EmbeddedSDCPool(properties);
    initialized = true;
}

From source file:com.hangum.tadpole.rdb.core.editors.main.composite.direct.SQLResultLabelProvider.java

/**
 * ??  , ??.//ww w .  j a va 2 s.  c o m
 * 
 * @param value
 * @return
 */
private static String addComma(Object value) {
    if (value == null)
        return PublicTadpoleDefine.DEFINE_NULL_VALUE;

    try {
        NumberFormat nf = NumberFormat.getNumberInstance();
        return nf.format(Double.parseDouble(value.toString()));
    } catch (Exception e) {
        // ignore exception
    }

    return value.toString();
}

From source file:WordCountSplitTest.java

private final static void test(boolean use_shards, boolean use_chunks, Boolean slaveok) throws Exception {
    did_start = false;/*ww w  . jav  a2 s .  c  om*/
    final Configuration conf = new Configuration();
    MongoConfigUtil.setInputURI(conf, "mongodb://localhost:30000/test.lines");
    conf.setBoolean(MongoConfigUtil.SPLITS_USE_SHARDS, use_shards);
    conf.setBoolean(MongoConfigUtil.SPLITS_USE_CHUNKS, use_chunks);
    String output_table = null;
    if (use_chunks) {
        if (use_shards)
            output_table = "with_shards_and_chunks";
        else
            output_table = "with_chunks";
    } else {
        if (use_shards)
            output_table = "with_shards";
        else
            output_table = "no_splits";
    }
    if (slaveok != null) {
        output_table += "_" + slaveok;
    }
    MongoConfigUtil.setOutputURI(conf, "mongodb://localhost:30000/test." + output_table);
    System.out.println("Conf: " + conf);

    final Job job = new Job(conf, "word count " + output_table);

    job.setJarByClass(WordCountSplitTest.class);

    job.setMapperClass(TokenizerMapper.class);

    job.setCombinerClass(IntSumReducer.class);
    job.setReducerClass(IntSumReducer.class);

    job.setOutputKeyClass(Text.class);
    job.setOutputValueClass(IntWritable.class);

    job.setInputFormatClass(MongoInputFormat.class);
    job.setOutputFormatClass(MongoOutputFormat.class);

    final long start = System.currentTimeMillis();
    System.out.println(" ----------------------- running test " + output_table + " --------------------");
    try {
        boolean result = job.waitForCompletion(true);
        System.out.println("job.waitForCompletion( true ) returned " + result);
    } catch (Exception e) {
        System.out.println("job.waitForCompletion( true ) threw Exception");
        e.printStackTrace();
    }
    final long end = System.currentTimeMillis();
    final float seconds = ((float) (end - start)) / 1000;
    java.text.NumberFormat nf = java.text.NumberFormat.getInstance();
    nf.setMaximumFractionDigits(3);
    System.out.println("finished run in " + nf.format(seconds) + " seconds");

    com.mongodb.Mongo m = new com.mongodb.Mongo(
            new com.mongodb.MongoURI("mongodb://localhost:30000/?slaveok=true"));
    com.mongodb.DB db = m.getDB("test");
    com.mongodb.DBCollection coll = db.getCollection(output_table);
    com.mongodb.BasicDBObject query = new com.mongodb.BasicDBObject();
    query.put("_id", "the");
    com.mongodb.DBCursor cur = coll.find(query);
    if (!cur.hasNext())
        System.out.println("FAILURE: could not find count of \'the\'");
    else
        System.out.println("'the' count: " + cur.next());

    //        if (! result)
    //           System.exit(  1 );
}