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

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Data Type » Number Format 
2.14.13.Get Percent ValuePrevious/Next
/*
 * 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 * 100100//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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.