Get Percent Value : Number Format « Data Type « Java Tutorial





/*
 * Cobertura - http://cobertura.sourceforge.net/
 *
 * Copyright (C) 2005 Jeremy Thomerson
 *
 * Note: This file is dual licensed under the GPL and the Apache
 * Source License (so that it can be used from both the main
 * Cobertura classes and the ant tasks).
 *
 * Cobertura is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 *
 * Cobertura is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cobertura; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */


import java.text.NumberFormat;

/**
 * Abstract, not to be instantiated utility class for String functions.
 * 
 * @author Jeremy Thomerson
 */
public abstract class StringUtil
{

  /**
   * 
   * Replaces all instances of "replace" with "with" from the "original"
   * string.
   * 
   *
   * 
   * NOTE: it is known that a similar function is included in jdk 1.4 as replaceAll(),
   * but is written here so as to allow backward compatibility to users using SDK's
   * prior to 1.4
   * 
   * 
   * @param original The original string to do replacement on.
   * @param replace The string to replace.
   * @param with The string to replace "replace" with.
   * @return The replaced string.
   */
  public static String replaceAll(String original, String replace, String with)
  {
    if (original == null)
    {
      return original;
    }

    final int len = replace.length();
    StringBuffer sb = new StringBuffer(original.length());
    int start = 0;
    int found = -1;

    while ((found = original.indexOf(replace, start)) != -1)
    {
      sb.append(original.substring(start, found));
      sb.append(with);
      start = found + len;
    }

    sb.append(original.substring(start));
    return sb.toString();
  }

  /**
   * Takes a double and turns it into a percent string.
   * Ex.  0.5 turns into 50%
   * 
   * @param value
   * @return corresponding percent string
   */
  public static String getPercentValue(double value)
  {
    //moved from HTMLReport.getPercentValue()
      value = Math.floor(value * 100) / 100; //to represent 199 covered lines from 200 as 99% covered, not 100 %
    return NumberFormat.getPercentInstance().format(value);
  }
  
}










2.14.Number Format
2.14.1.Number formatting helps make your numbers more readable.
2.14.2.Specifying Precision
2.14.3.Applied to strings, the precision specifier specifies the maximum field length
2.14.4.Illustrating the precision specifier
2.14.5.Add leading zeros to a number
2.14.6.NumberFormat.getInstance()
2.14.7.NumberFormat.getCurrencyInstance(Locale.ENGLISH)
2.14.8.NumberFormat: Minimum Integer Digits, Maximum/Minimum Fraction Digits
2.14.9.Number format with FieldPosition
2.14.10.NumberFormat.getPercentInstance(Locale.ENGLISH)
2.14.11.A number formatter for logarithmic values. This formatter does not support parsing.
2.14.12.Format a percentage for presentation to the user
2.14.13.Get Percent Value
2.14.14.Helper class for format number and currency