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

/**
 * Method setDoubleAttribute/*  www  .j av  a  2  s.  c  o m*/
 * 
 * @param element
 * @param attributeName
 * @param value
 */
public static void setDoubleAttribute(Element element, String attributeName, double value) {
    element.setAttribute(attributeName, Double.toString(value));
}

From source file:Main.java

public static double round(double d, int decimalPlace) {
    // see the Javadoc about why we use a String in the constructor
    // http://java.sun.com/j2se/1.5.0/docs/api/java/math/BigDecimal.html#BigDecimal(double)
    BigDecimal bd = new BigDecimal(Double.toString(d));
    bd = bd.setScale(decimalPlace, BigDecimal.ROUND_HALF_UP);
    return bd.doubleValue();
}

From source file:Main.java

/**
 * replace point with comma in double values
 *
 * @param double value - ouble value with point as decimal point
 * @return String - string representation of the double value with comma instead of point
 *//*from  w  ww.ja  v  a2 s . c  om*/
public static String pointToComma(double value) {
    return Double.toString(value).replace(".", ",");
}

From source file:Main.java

public static double roundDouble(double v, int scale) {

    if (scale < 0) {

        throw new IllegalArgumentException(

                "The scale must be a positive integer or zero");

    }/*w w  w  . j a v  a2s.c  om*/

    BigDecimal b = new BigDecimal(Double.toString(v));

    BigDecimal one = new BigDecimal("1");

    return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();

}

From source file:ReadTemp.java

/**
 * Method main//from  w ww .  j ava 2s .co  m
 *
 *
 * @param args
 *
 * @throws OneWireException
 * @throws OneWireIOException
 *
 */
public static void main(String[] args) throws OneWireIOException, OneWireException {
    boolean usedefault = false;
    DSPortAdapter access = null;
    String adapter_name = null;
    String port_name = null;

    variableNames = new HashMap();

    if ((args == null) || (args.length < 1)) {
        try {
            access = OneWireAccessProvider.getDefaultAdapter();

            if (access == null)
                throw new Exception();
        } catch (Exception e) {
            System.out.println("Couldn't get default adapter!");
            printUsageString();

            return;
        }

        usedefault = true;
    }

    if (!usedefault) {
        StringTokenizer st = new StringTokenizer(args[0], "_");

        if (st.countTokens() != 2) {
            printUsageString();

            return;
        }

        adapter_name = st.nextToken();
        port_name = st.nextToken();

        System.out.println("Adapter Name: " + adapter_name);
        System.out.println("Port Name: " + port_name);
    }

    if (access == null) {
        try {
            access = OneWireAccessProvider.getAdapter(adapter_name, port_name);
        } catch (Exception e) {
            System.out.println("That is not a valid adapter/port combination.");

            Enumeration en = OneWireAccessProvider.enumerateAllAdapters();

            while (en.hasMoreElements()) {
                DSPortAdapter temp = (DSPortAdapter) en.nextElement();

                System.out.println("Adapter: " + temp.getAdapterName());

                Enumeration f = temp.getPortNames();

                while (f.hasMoreElements()) {
                    System.out.println("   Port name : " + ((String) f.nextElement()));
                }
            }

            return;
        }
    }

    boolean scan = false;

    if (args.length > 1) {
        if (args[1].compareTo("--scan") == 0)
            scan = true;
    } else {
        populateVariableNames();
    }

    while (true) {
        access.adapterDetected();
        access.targetAllFamilies();
        access.beginExclusive(true);
        access.reset();
        access.setSearchAllDevices();

        boolean next = access.findFirstDevice();

        if (!next) {
            System.out.println("Could not find any iButtons!");

            return;
        }

        while (next) {
            OneWireContainer owc = access.getDeviceContainer();

            boolean isTempContainer = false;
            TemperatureContainer tc = null;

            try {
                tc = (TemperatureContainer) owc;
                isTempContainer = true;
            } catch (Exception e) {
                tc = null;
                isTempContainer = false; //just to reiterate
            }

            if (isTempContainer) {
                String id = owc.getAddressAsString();
                if (scan) {
                    System.out.println("= Temperature Sensor Found: " + id);
                } else {

                    double high = 0.0;
                    double low = 0.0;
                    byte[] state = tc.readDevice();

                    boolean selectable = tc.hasSelectableTemperatureResolution();

                    if (selectable)
                        try {
                            tc.setTemperatureResolution(0.0625, state);
                        } catch (Exception e) {
                            System.out.println("= Could not set resolution for " + id + ": " + e.toString());
                        }

                    try {
                        tc.writeDevice(state);
                    } catch (Exception e) {
                        System.out.println("= Could not write device state, all changes lost.");
                        System.out.println("= Exception occurred for " + id + ": " + e.toString());
                    }

                    boolean conversion = false;
                    try {
                        tc.doTemperatureConvert(state);
                        conversion = true;
                    } catch (Exception e) {
                        System.out.println("= Could not complete temperature conversion...");
                        System.out.println("= Exception occurred for " + id + ": " + e.toString());
                    }

                    if (conversion) {
                        state = tc.readDevice();

                        double temp = tc.getTemperature(state);
                        if (temp < 84) {
                            double temp_f = (9.0 / 5.0) * temp + 32;
                            setVariable(id, Double.toString(temp_f));
                        }
                        System.out.println("= Reported temperature from " + (String) variableNames.get(id) + "("
                                + id + "):" + temp);
                    }
                }
            }
            next = access.findNextDevice();
        }
        if (!scan) {
            try {
                Thread.sleep(60 * 5 * 1000);
            } catch (Exception e) {
            }
        } else {
            System.exit(0);
        }
    }
}

From source file:Main.java

/**
 * Formats the double as a string which is cleaned up a bit. This will return 0 for 0.0.
 * /*  w  w w .ja va 2 s .  c o m*/
 * @param num Number to be formatted.
 * @return Number as a string.
 */
private static String formatAsString(double num) {
    String res = Double.toString(num);

    if (res.endsWith(".0")) {
        res = res.substring(0, res.length() - 2);
    }

    if (res.equals("-0")) {
        res = "0";
    }

    return res;
}

From source file:Main.java

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

From source file:Main.java

public static String getFormatSize(double size) {
    if (0 == size) {
        return "0K";
    }/*w  w  w. j av  a2s  . c o m*/

    double kiloByte = size / 1024;
    double megaByte = kiloByte / 1024;
    if (megaByte < 1) {
        BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
        return result1.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString() + "K";
    }

    double gigaByte = megaByte / 1024;
    if (gigaByte < 1) {
        BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
        return result2.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString() + "M";
    }

    double teraBytes = gigaByte / 1024;
    if (teraBytes < 1) {
        BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
        return result3.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString() + "G";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(2, BigDecimal.ROUND_HALF_UP).stripTrailingZeros().toPlainString() + "T";
}

From source file:Main.java

private static String getFormatSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "Byte";
    }/*  w  w  w .j a va2 s . c o  m*/
    double megaByte = kiloByte / 1024;
    if (megaByte < 1) {
        BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
        return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
    }
    double gigaByte = megaByte / 1024;
    if (gigaByte < 1) {
        BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
        return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
    }
    double teraBytes = gigaByte / 1024;
    if (teraBytes < 1) {
        BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
        return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
}

From source file:Main.java

public static String formatFileSize(double size) {
    double kiloByte = size / 1024;
    if (kiloByte < 1) {
        return size + "B";
    }/*  w w w  . ja v a  2  s.co m*/

    double megaByte = kiloByte / 1024;
    if (megaByte < 1) {
        BigDecimal result1 = new BigDecimal(Double.toString(kiloByte));
        return result1.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "KB";
    }

    double gigaByte = megaByte / 1024;
    if (gigaByte < 1) {
        BigDecimal result2 = new BigDecimal(Double.toString(megaByte));
        return result2.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "MB";
    }

    double teraBytes = gigaByte / 1024;
    if (teraBytes < 1) {
        BigDecimal result3 = new BigDecimal(Double.toString(gigaByte));
        return result3.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "GB";
    }
    BigDecimal result4 = new BigDecimal(teraBytes);
    return result4.setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "TB";
}