Java String Chop chopFractionalPart(float original, int no_of_digits )

Here you can find the source of chopFractionalPart(float original, int no_of_digits )

Description

Limitation: (original * 10^no_of_digits) has to fit in a signed integer (java integers are 32bit)

License

Open Source License

Declaration

public static final float chopFractionalPart(float original, int no_of_digits 
) 

Method Source Code

//package com.java2s;
/* /*from  w ww .  ja v a2  s  .c om*/
  This file is part of JIV.  
  Copyright (C) 2000, 2001 Chris A. Cocosco (crisco@bic.mni.mcgill.ca)
    
  JIV 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.
    
  JIV 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 JIV; if not, write to the Free Software Foundation, Inc.,
  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA, 
  or see http://www.gnu.org/copyleft/gpl.html
*/

public class Main {
    /** used by reduceSignificantDigits() */
    /*private*/ static final int[] __chopFractionalPart_lookup = { 1, 10, 100, 1000, 10000, 100000 };

    /** Limitation: (original * 10^no_of_digits) has to fit in a
    signed integer (java integers are 32bit)
    */
    public static final float chopFractionalPart(float original, int no_of_digits /** 0...5 */
    ) {
        // use a lookup table for speed
        int mult = __chopFractionalPart_lookup[no_of_digits];
        return Math.round(original * mult) / (float) mult;
    }
}

Related

  1. ChopAllRt(String this_string, String chomp_off)
  2. chopBraces(String s)
  3. chopCommentFromArgs(String[] args)
  4. chopExt(String orig)
  5. chopFirstComponent(String s)
  6. chopId(String name)
  7. chopIfMatch(StringBuilder sb, char ch)
  8. chopLast(String value, int chars)
  9. chopLenghtyString(String title, int maxLen)