Example usage for java.lang Double toString

List of usage examples for java.lang Double toString

Introduction

In this page you can find the example usage for java.lang Double toString.

Prototype

public static String toString(double d) 

Source Link

Document

Returns a string representation of the double argument.

Usage

From source file:Main.java

public static String doubleToString(double value) {
    return Double.toString(Math.floor(value * 10000.0 + 0.5) / 10000.0);
}

From source file:Main.java

/**
 * This used when a double ends in 0 (IE 100.0) and you want 2 decimal places instead (IE 100.00)
 * @param value The double to convert/*ww w .ja v  a  2  s .com*/
 * @param addDollarSign boolean, if null passed, nothing, if true passed, will add
 *                      a $ to the begining
 * @return A String, formatted correctly. Will look like this: 104.44 or $99.40
 */
public static String convertDoubleToStringAddZeroNoRemove(double value, Boolean addDollarSign) {
    String str = Double.toString(value);
    String ending = str.substring((str.length() - 2), (str.length() - 1));
    if (ending.equalsIgnoreCase(".")) {
        str = str + "0";
    }
    if (addDollarSign != null) {
        if (addDollarSign) {
            str = "$" + str;
        }
    }
    return str;
}

From source file:Main.java

public static double mul(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.multiply(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double add(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.add(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

public static double sub(double v1, double v2) {
    BigDecimal b1 = new BigDecimal(Double.toString(v1));
    BigDecimal b2 = new BigDecimal(Double.toString(v2));
    return b1.subtract(b2).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
}

From source file:Main.java

/**
 * This used when a double ends in 0 (IE 100.0) and you want to remove the significant figures
 * after the decimal point (assuming they end in zero)
 * @param value The double to convert//from  w  w  w .j a va 2s . c  o m
 * @param addDollarSign boolean, if null passed, nothing, if true passed, will add
 *                      a $ to the beginning
 * @return A String, formatted correctly. Will look like this: 104.44 or $99.
 *         if the last 2 are 00, it will remove the significant figures after
 *         the decimal as well as the decimal itself
 */
public static String convertDoubleToStringAddZero(double value, Boolean addDollarSign) {
    String str = Double.toString(value);
    if (str == null) {
        return null;
    }
    //String ending = str.substring((str.length()-2));
    String ending = str.substring((str.length() - 2), (str.length() - 1));
    if (ending.equalsIgnoreCase(".")) {
        str = str + "0";
    }
    if (addDollarSign != null) {
        if (addDollarSign) {
            str = "$" + str;
        }
    }
    ending = str.substring((str.length() - 3));
    if (ending.equalsIgnoreCase(".00")) {
        str = str.replace(".00", "");
    }

    return str;
}

From source file:TimeFormatUtil.java

/**
 * @param seconds/*www .j  av  a2s.  co  m*/
 * @return a string which represents a floating point number with max. 2
 *         decimals
 */
public static String formatTimeTo2Digits(double seconds) {
    String timeString = Double.toString(seconds);

    int dotPos = timeString.indexOf(".");
    if (dotPos < 0)
        return timeString;

    Pattern p = Pattern.compile("([0-9]{1,})\\.([0-9]{1,})");
    Matcher m = p.matcher(timeString);
    if (!m.matches())
        throw new RuntimeException(
                "WARNING: pattern '" + p.pattern() + "' " + "did not match input '" + timeString + "'");

    StringBuilder b = new StringBuilder(m.group(1));
    b.append('.');
    String afterCommaDigits = m.group(2);

    if (afterCommaDigits.length() > 2)
        b.append(m.group(2).substring(0, 2));
    else
        b.append(afterCommaDigits);

    return b.toString();
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    try {/*w  ww  .ja  v a2s  .  co  m*/
        BigDecimal bd = new BigDecimal(Double.toString(d));
        bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    } catch (NumberFormatException e) {
        //on NaN or +/- infinity return the value
        return d;
    }
}

From source file:Main.java

/**
 * Convert to String a double value that received as parameter.
 *
 * @param doub the doub//from   w w w  . j  a  v a2s.c o  m
 * @return String
 */
public static String toStringDouble(Double doub) {
    return doub == null ? null : URLEncoder.encode(Double.toString(doub).replace(',', '.'));
}

From source file:Main.java

public static void writeDoubleAttr(Element element, String attributeName, double value) {
    element.setAttribute(attributeName, Double.toString(value));
}