Parse BigDecimal : BigDecimal « Data Type « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Data Type » BigDecimal 




Parse BigDecimal
    
/*
 * This file is part of JGAP.
 *
 * JGAP offers a dual license model containing the LGPL as well as the MPL.
 *
 * For licensing information please see the file license.txt included with JGAP
 * or have a look at the top of class org.jgap.Chromosome which representatively
 * includes the JGAP license policy applicable for any file delivered with JGAP.
 */


/**
 * Utility routines related to numbers.
 *
 @author Klaus Meffert
 @since 3.2
 */
public class NumberKit {


  /**
   * Returns the hex value of "c" or -1 if there is no corresponding hex value.
   *
   @param a_c hex character to convert
   @return integer value of the character
   *
   @author unknown
   @since 3.2
   */
  public static int hexValue(char a_c) {
    if ('0' <= a_c && a_c <= '9') {
      return a_c - '0';
    }
    if ('A' <= a_c && a_c <= 'F') {
      return a_c - 'A' 10;
    }
    if ('a' <= a_c && a_c <= 'f') {
      return a_c - 'a' 10;
    }
    return -1;
  }

  private static final char[] DIGITS = {'0''1''2''3''4''5''6',
      '7''8''9''a''b''c''d''e''f'};

  /**
   * Transforms a byte to a character array of hex octets.
   * Taken from UUID.
   *
   @param a_in the byte
   @return the hex byte array
   */
  public static char[] asChars(byte a_in) {
    return asChars(a_in, 2);
  }

  /**
   * Transforms a byte to a character array of hex octets.
   * Taken from UUID.
   *
   @param a_in the byte
   @param a_length the number of octets to produce
   @return the hex byte array
   */
  public static char[] asChars(byte a_in, int a_length) {
    char[] out = new char[a_length--];
    for (int i = a_length; i > -1; i--) {
      out[i= DIGITS[ (byte) (a_in & 0x0F)];
      a_in >>= 4;
    }
    return out;
  }

  /**
   * Parses a short from a hex encoded number. This method will skip
   * all characters that are not 0-9 and a-f (the String is lower cased first).
   *
   @param s the String to extract a short from, may not be null
   @return 0 if the String does not contain any interesting characters
   @throws NullPointerException if the String is null
   *
   @since 3.3.3
   */
  public static short parseShort(String s)
      throws NullPointerException {
    s = s.toLowerCase();
    short out = 0;
    byte shifts = 0;
    char c;
    for (int i = 0; i < s.length() && shifts < 4; i++) {
      c = s.charAt(i);
      if ( (c > 47&& (c < 58)) {
        out <<= 4;
        ++shifts;
        out |= c - 48;
      }
      else if ( (c > 96&& (c < 103)) {
        ++shifts;
        out <<= 4;
        out |= c - 87;
      }
    }
    return out;
  }

  /**
   * Formats a number as a string having the total length of a_places, filling
   * up needed characters with a_filler.
   *
   @param a_number the number to format
   @param a_places total length of output string
   @param a_filler fill character
   @return formatted number
   *
   @author Klaus Meffert
   @since 3.3.3
   */
  public static String niceNumber(int a_number, int a_places, char a_filler) {
    String s = a_number + "";
    while (s.length() < a_places) {
      s = a_filler + s;
    }
    return s;
  }

  /**
   * Removes decimal places if there are more than a_decimals.
   *
   @param a_number the number to convert to a string
   @param a_decimals maximum number of decimal places allowed
   *
   @return nicified string
   *
   @author Klaus Meffert
   @since 3.3.3
   */
  public static String niceDecimalNumber(double a_number, int a_decimals) {
    String s = a_number + "";
    int index = s.indexOf('.');
    if (index > 0) {
      // Do not remove anything if "E" is contained in the number string.
      // ----------------------------------------------------------------
      if (s.indexOf('E',index0) {
        return s;
      }
      if (index + a_decimals >= s.length()) {
        a_decimals = s.length() - index - 1;
      }
      s = s.substring(0, index + a_decimals + 1);
      if (s.lastIndexOf('.'== s.length() 1) {
        if (a_decimals < 1) {
          s = s.substring(0, s.length() 1);
        }
        else {
          for (int i = 0; i < a_decimals; i++) {
            s += "0";
          }
        }
      }
    }
    return s;
  }
}

   
    
    
    
  














Related examples in the same category
1.Round a double
2.Create Big Decimal Values via a long
3.Create a BigDecimal vis string
4.Multiply one BigDecimal to another BigDecimal
5.Subtract from one BigDecimal another BigDecimal
6.Divide one BigDecimal from another BigDecimal
7.Negate a BigDecimal
8.Setting the Decimal Place of a Big Decimal Value
9.Truncates the big decimal value
10.Do math operation for BigDecimal
11.Operate with big decimal values
12.Round a double by setting the scale
13.Create Big Decimal Values via a string
14.Calculation with BigDecimal
15.Formats BigDecimal into a SQL floating-point literal
16.Value is rounded using the given method which is any method defined in BigDecimal
17.Round the given value to the specified number of decimal places. The value is rounded using the BigDecimal.ROUND_HALF_UP method.
18.Convert Object to BigDecimal
19.BigDecimal and BigInteger sqare root
20.BigDecimal quadratic
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.