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.redhat.rhn.common.localization.LocalizationService.java

/**
 * Format the Number based on the locale and convert it to a String to
 * display. Use a specified number of fractional digits.
 * @param numberIn Number to format.//  ww  w  .ja va 2s . c  om
 * @param localeIn Locale to use for formatting.
 * @param fractionalDigits The maximum number of fractional digits to use.
 * @return String representation of given number in given locale.
 */
public String formatNumber(Number numberIn, Locale localeIn, int fractionalDigits) {
    NumberFormat nf = NumberFormat.getInstance(localeIn);
    nf.setMaximumFractionDigits(fractionalDigits);
    return getDebugVersionOfString(nf.format(numberIn));
}

From source file:com.prowidesoftware.swift.model.field.Field34F.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8//w w w  . j a  va2s .  co m
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 3) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 34F");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //default format (as is)
        return getComponent(2);
    }
    if (component == 3) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent3AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:com.prowidesoftware.swift.model.field.Field34G.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8/*w  w  w .  j  a va2 s .c o  m*/
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 3) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 34G");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent1AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    if (component == 2) {
        //default format (as is)
        return getComponent(2);
    }
    if (component == 3) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent3AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:itdelatrisu.opsu.replay.Replay.java

/**
 * Saves the replay data to a file in the replays directory.
 *//*from w ww .j  a v a2 s. c o  m*/
public void save() {
    // create replay directory
    File dir = Options.getReplayDir();
    if (!dir.isDirectory()) {
        if (!dir.mkdir()) {
            ErrorHandler.error("Failed to create replay directory.", null, false);
            return;
        }
    }

    // write file in new thread
    final File file = new File(dir, String.format("%s.osr", getReplayFilename()));
    new Thread() {
        @Override
        public void run() {
            try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
                OsuWriter writer = new OsuWriter(out);

                // header
                writer.write(mode);
                writer.write(version);
                writer.write(beatmapHash);
                writer.write(playerName);
                writer.write(replayHash);
                writer.write(hit300);
                writer.write(hit100);
                writer.write(hit50);
                writer.write(geki);
                writer.write(katu);
                writer.write(miss);
                writer.write(score);
                writer.write(combo);
                writer.write(perfect);
                writer.write(mods);

                // life data
                StringBuilder sb = new StringBuilder();
                if (lifeFrames != null) {
                    NumberFormat nf = new DecimalFormat("##.##");
                    for (int i = 0; i < lifeFrames.length; i++) {
                        LifeFrame frame = lifeFrames[i];
                        sb.append(String.format("%d|%s,", frame.getTime(), nf.format(frame.getPercentage())));
                    }
                }
                writer.write(sb.toString());

                // timestamp
                writer.write(timestamp);

                // LZMA-encoded replay data
                if (frames != null && frames.length > 0) {
                    // build full frame string
                    NumberFormat nf = new DecimalFormat("###.#####");
                    sb = new StringBuilder();
                    for (int i = 0; i < frames.length; i++) {
                        ReplayFrame frame = frames[i];
                        sb.append(String.format("%d|%s|%s|%d,", frame.getTimeDiff(), nf.format(frame.getX()),
                                nf.format(frame.getY()), frame.getKeys()));
                    }
                    sb.append(String.format("%s|0|0|%d", SEED_STRING, seed));

                    // get bytes from string
                    CharsetEncoder encoder = StandardCharsets.US_ASCII.newEncoder();
                    CharBuffer buffer = CharBuffer.wrap(sb);
                    byte[] bytes = encoder.encode(buffer).array();

                    // compress data
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    LzmaOutputStream compressedOut = new LzmaOutputStream.Builder(bout)
                            .useMediumDictionarySize().build();
                    try {
                        compressedOut.write(bytes);
                    } catch (IOException e) {
                        // possible OOM: https://github.com/jponge/lzma-java/issues/9
                        ErrorHandler.error("LZMA compression failed (possible out-of-memory error).", e, true);
                    }
                    compressedOut.close();
                    bout.close();

                    // write to file
                    byte[] compressed = bout.toByteArray();
                    writer.write(compressed.length);
                    writer.write(compressed);
                } else
                    writer.write(0);

                writer.close();
            } catch (IOException e) {
                ErrorHandler.error("Could not save replay data.", e, true);
            }
        }
    }.start();
}

From source file:com.prowidesoftware.swift.model.field.Field32H.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8//from   ww  w  .  ja  v a2s.  c  o m
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 3) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 32H");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //default format (as is)
        return getComponent(2);
    }
    if (component == 3) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent3AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:com.prowidesoftware.swift.model.field.Field34E.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8//from ww  w.  ja va  2s .c  o m
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 3) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 34E");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //default format (as is)
        return getComponent(2);
    }
    if (component == 3) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent3AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:com.datasalt.pangool.solr.SolrRecordWriter.java

private String getOutFileName(TaskAttemptContext context, String prefix) {
    TaskID taskId = context.getTaskAttemptID().getTaskID();
    int partition = taskId.getId();
    NumberFormat nf = NumberFormat.getInstance();
    nf.setMinimumIntegerDigits(5);//from   w  ww  .  j  a v a2s.  c  o  m
    nf.setGroupingUsed(false);
    StringBuilder result = new StringBuilder();
    result.append(prefix);
    result.append("-");
    result.append(nf.format(partition));
    return result.toString();
}

From source file:com.prowidesoftware.swift.model.field.Field92D.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8/*from  ww  w.  j  a  va 2 s .  c  o m*/
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 3) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 92D");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    if (component == 3) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent3AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    return null;
}

From source file:semanticweb.hws14.movapp.activities.MovieDetail.java

private void setData(final MovieDet movie) {
    Thread picThread = new Thread(new Runnable() {
        public void run() {
            try {
                ImageView img = (ImageView) findViewById(R.id.imageViewMovie);
                URL url = new URL(movie.getPoster());
                HttpGet httpRequest;/*from  ww w . ja  va 2s .com*/

                httpRequest = new HttpGet(url.toURI());

                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httpRequest);

                HttpEntity entity = response.getEntity();
                BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
                InputStream input = b_entity.getContent();

                Bitmap bitmap = BitmapFactory.decodeStream(input);

                img.setImageBitmap(bitmap);

            } catch (Exception ex) {
                Log.d("MovieDetail Picture Error ", ex.toString());
            }
        }
    });
    picThread.start();

    if (!"".equals(movie.getTitle())) {
        TextView movieTitle = (TextView) findViewById(R.id.tvMovieTitle);
        movieTitle.setText(movie.getTitle());
        movieTitle.setVisibility(View.VISIBLE);
    }

    if (!"".equals(movie.getPlot())) {
        TextView moviePlot = (TextView) findViewById(R.id.tvPlot);
        moviePlot.setText(movie.getPlot());
        moviePlot.setVisibility(View.VISIBLE);
    }

    TextView ageRestriction = (TextView) findViewById(R.id.tvAgeRestriction);
    TextView arHc = (TextView) findViewById(R.id.tvAgeRestrictionHC);
    String aR = String.valueOf(movie.getRated());

    //Decode the ageRestriction
    if (!aR.equals("")) {
        ageRestriction.setVisibility(View.VISIBLE);
        arHc.setVisibility(View.VISIBLE);
        if (aR.equals("X")) {
            ageRestriction.setText("18+");
        } else if (aR.equals("R")) {
            ageRestriction.setText("16+");
        } else if (aR.equals("M")) {
            ageRestriction.setText("12+");
        } else if (aR.equals("G")) {
            ageRestriction.setText("6+");
        } else {
            ageRestriction.setVisibility(View.GONE);
            arHc.setVisibility(View.GONE);
        }
    }

    TextView ratingCount = (TextView) findViewById(R.id.tvMovieRatingCount);
    TextView ratingCountHc = (TextView) findViewById(R.id.tvMovieRatingCountHC);
    String ratingCountText = movie.getVoteCount();
    ratingCount.setText(ratingCountText);
    manageEmptyTextfields(ratingCountHc, ratingCount, ratingCountText, false);

    if (!"".equals(movie.getImdbRating())) {
        TextView movieRating = (TextView) findViewById(R.id.tvMovieRating);
        if (movie.getImdbRating().equals("0 No Rating")) {
            movieRating.setText("No Rating");
        } else if (movie.getImdbRating().equals("0 No Data")) {
            movieRating.setText("");
        } else {
            movieRating.setText(movie.getImdbRating() + "/10");
        }
        movieRating.setVisibility(View.VISIBLE);
        findViewById(R.id.tvMovieRatingHC).setVisibility(View.VISIBLE);
    }

    TextView metaScore = (TextView) findViewById(R.id.tvMetaScore);
    TextView metaScoreHc = (TextView) findViewById(R.id.tvMetaScoreHC);
    String metaSoreText = String.valueOf(movie.getMetaScore());
    metaScore.setText(metaSoreText + "/100");
    manageEmptyTextfields(metaScoreHc, metaScore, metaSoreText, false);

    TextView tvGenreHc = (TextView) findViewById(R.id.tvGenreHC);
    TextView genre = (TextView) findViewById(R.id.tvGenre);
    String genreText = String.valueOf(movie.createTvOutOfList(movie.getGenres()));
    genre.setText(genreText);
    manageEmptyTextfields(tvGenreHc, genre, genreText, true);

    TextView releaseYear = (TextView) findViewById(R.id.tvReleaseYear);
    TextView releaseYearHc = (TextView) findViewById(R.id.tvReleaseYearHC);
    String releaseYearText = String.valueOf(movie.getReleaseYear());
    releaseYear.setText(releaseYearText);
    manageEmptyTextfields(releaseYearHc, releaseYear, releaseYearText, true);

    TextView runtime = (TextView) findViewById(R.id.tvRuntime);
    TextView runTimeHc = (TextView) findViewById(R.id.tvRuntimeHC);
    String runtimeText = "";
    try {
        runtimeText = runtimeComputation(Float.parseFloat(movie.getRuntime()));
    } catch (Exception e) {
        runtimeText = movie.getRuntime();
    }
    runtime.setText(runtimeText);
    manageEmptyTextfields(runTimeHc, runtime, runtimeText, true);

    TextView budget = (TextView) findViewById(R.id.tvBudget);
    TextView budgetHc = (TextView) findViewById(R.id.tvBudgetHC);
    String budgetText = movie.getBudget();
    //Decode the budget
    if (budgetText.contains("E")) {
        BigDecimal myNumber = new BigDecimal(budgetText);
        long budgetLong = myNumber.longValue();
        NumberFormat formatter = NumberFormat.getCurrencyInstance(Locale.US);
        budgetText = formatter.format(budgetLong);
        budgetText = String.valueOf(budgetText);
        budgetText = budgetComputation(budgetText);
        budget.setText(budgetText);
        manageEmptyTextfields(budgetHc, budget, budgetText, true);
    }

    TextView awards = (TextView) findViewById(R.id.tvAwards);
    TextView awardsHc = (TextView) findViewById(R.id.tvAwardsHC);
    String awardsText = movie.getAwards();
    awards.setText(awardsText);
    manageEmptyTextfields(awardsHc, awards, awardsText, true);

    TextView tvDirHc = (TextView) findViewById(R.id.tvDirectorsHC);
    TextView directors = (TextView) findViewById(R.id.tvDirectors);
    String directorText = String.valueOf(movie.createTvOutOfList(movie.getDirectors()));
    directors.setText(directorText);
    manageEmptyTextfields(tvDirHc, directors, directorText, true);

    TextView tvWriterHc = (TextView) findViewById(R.id.tvWritersHC);
    TextView writers = (TextView) findViewById(R.id.tvWriters);
    String writerText = String.valueOf(movie.createTvOutOfList(movie.getWriters()));
    writers.setText(writerText);
    manageEmptyTextfields(tvWriterHc, writers, writerText, true);

    TextView tvActorsHc = (TextView) findViewById(R.id.tvActorsHC);
    TextView actors = (TextView) findViewById(R.id.tvActors);
    String actorText = String.valueOf(movie.createTvOutOfList(movie.getActors()));
    actors.setText(actorText);
    manageEmptyTextfields(tvActorsHc, actors, actorText, true);
    colorIt(actors);

    if (movie.getActors().size() > 0) {
        btnActorList.setVisibility(View.VISIBLE);
    }

    btnRandomRelatedMovies.setVisibility(View.VISIBLE);
    btnRelatedMovies.setVisibility(View.VISIBLE);

    TextView tvRolesHc = (TextView) findViewById(R.id.tvRolesHC);
    TextView roles = (TextView) findViewById(R.id.tvRoles);
    ArrayList<String> roleList = movie.getRoles();

    if (roleList.size() > 0) {
        roles.setVisibility(View.VISIBLE);
        tvRolesHc.setVisibility(View.VISIBLE);
        roles.setText(String.valueOf(movie.createTvOutOfList(roleList)));
    }

    TextView wikiAbstract = (TextView) findViewById(R.id.tvWikiAbstract);
    wikiAbstract.setText(movie.getWikiAbstract());
    if (!"".equals(movie.getImdbId())) {
        btnImdbPage.setVisibility(View.VISIBLE);
    }
    if (!"".equals(movie.getWikiAbstract())) {
        btnSpoiler.setVisibility(View.VISIBLE);
    }
    try {
        picThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    setProgressBarIndeterminateVisibility(false);
}

From source file:com.prowidesoftware.swift.model.field.Field14S.java

/**
 * Returns a localized suitable for showing to humans string of a field component.<br>
 *
 * @param component number of the component to display
 * @param locale optional locale to format date and amounts, if null, the default locale is used
 * @return formatted component value or null if component number is invalid or not present
 * @throws IllegalArgumentException if component number is invalid for the field
 * @since 7.8// w  ww  . jav a  2  s . c  o  m
 */
@Override
public String getValueDisplay(int component, Locale locale) {
    if (component < 1 || component > 4) {
        throw new IllegalArgumentException("invalid component number " + component + " for field 14S");
    }
    if (locale == null) {
        locale = Locale.getDefault();
    }
    if (component == 1) {
        //default format (as is)
        return getComponent(1);
    }
    if (component == 2) {
        //number or amount
        java.text.NumberFormat f = java.text.NumberFormat.getNumberInstance(locale);
        Number n = getComponent2AsNumber();
        if (n != null) {
            return f.format(n);
        }
    }
    if (component == 3) {
        //time
        java.text.DateFormat f = new java.text.SimpleDateFormat("HH:mm", locale);
        java.util.Calendar cal = getComponent3AsCalendar();
        if (cal != null) {
            return f.format(cal.getTime());
        }
    }
    if (component == 4) {
        //default format (as is)
        return getComponent(4);
    }
    return null;
}