All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.mines.jtk.util.StringUtil Maven / Gradle / Ivy

There is a newer version: 1.1.0
Show newest version
/****************************************************************************
Copyright 2008, Colorado School of Mines and others.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
****************************************************************************/
package edu.mines.jtk.util;

/**
 * Utilities for manipulating strings of characters.
 * @author Dave Hale, Colorado School of Mines
 */
public class StringUtil {

  /**
   * Removes any trailing zeros from a string representing a float or double.
   * Also removes any insignificant trailing decimal point. For example,
   * converts the string "1.00" to "1"; converts the string "1.2300e-6" 
   * to "1.23e-6".
   * @param s a string representing a float or double.
   * @return the string with insignificant trailing zeros removed.
   */
  public static String removeTrailingZeros(String s) {
    int len = s.length();
    int iend = s.indexOf('e');
    if (iend<0)
      iend = s.indexOf('E');
    if (iend<0)
      iend = len;
    int ibeg = iend;
    if (s.indexOf('.')>0) {
      while (ibeg>0 && s.charAt(ibeg-1)=='0')
        --ibeg;
      if (ibeg>0 && s.charAt(ibeg-1)=='.')
        --ibeg;
    }
    if (ibeg




© 2015 - 2024 Weber Informatics LLC | Privacy Policy