Example usage for java.lang Number doubleValue

List of usage examples for java.lang Number doubleValue

Introduction

In this page you can find the example usage for java.lang Number doubleValue.

Prototype

public abstract double doubleValue();

Source Link

Document

Returns the value of the specified number as a double .

Usage

From source file:Main.java

public static void main(String[] args) {
    System.out.println("Enter a number.");
    double numberFromConsole;
    try {/*from   w w  w  . ja  v a2 s.  c  o  m*/
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        String s = br.readLine();
        DecimalFormat df = new DecimalFormat();
        Number n = df.parse(s);
        numberFromConsole = n.doubleValue();
    } catch (IOException e) {
        numberFromConsole = 0;
    } catch (ParseException e) {
        numberFromConsole = 0;
    }
    System.out.println(numberFromConsole);
}

From source file:Main.java

public static void main(String[] args) {
    // Parse a string to decimal number
    String str = "qq1,234.567";
    String pattern = "#,###.###";
    formatter.applyPattern(pattern);// w w w . jav  a2 s  .c o  m

    // Create a ParsePosition object to specify the first digit of
    // number in the string. It is 1 in "qq1,234.567"
    // with the index 2.
    ParsePosition pp = new ParsePosition(2);

    Number numberObject = formatter.parse(str, pp);

    double value = numberObject.doubleValue();
    System.out.println("Parsed Value  is " + value);
}

From source file:Main.java

public static void main(String[] args) {
    DecimalFormat format = new DecimalFormat("####.##");
    format.setMinimumFractionDigits(2);/*from   w ww .j  a va 2  s.c  om*/
    final JFormattedTextField field1 = new JFormattedTextField(format);
    final JFormattedTextField field2 = new JFormattedTextField(format);
    field1.setColumns(15);
    field2.setColumns(15);
    JButton btn = new JButton(new AbstractAction("Multiply by 2") {

        @Override
        public void actionPerformed(ActionEvent e) {
            Number value = (Number) field1.getValue();
            if (value != null) {
                field2.setValue(2 * value.doubleValue());
            }
        }
    });

    JPanel panel = new JPanel();
    panel.add(field1);
    panel.add(btn);
    panel.add(field2);
    JOptionPane.showMessageDialog(null, panel);
}

From source file:Main.java

public static void main(String[] args) {

    // get a number as integer
    Number x = new Integer(123456);

    // get a number as float
    Number y = new Float(9876f);

    // print their value as double
    System.out.println("x as integer :" + x + ", x as double:" + x.doubleValue());
    System.out.println("y as float::" + y + ", y as double:" + x.doubleValue());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Number number = NumberFormat.getNumberInstance(Locale.JAPAN).parse("25,000.75");
    if (number instanceof Long) {
        System.out.println("Long value: " + number.longValue());
    } else if (number instanceof Double) {
        System.out.println("Double value: " + number.doubleValue());
    }//from   ww w .  j  a  v  a 2  s .c  o m
}

From source file:Main.java

public static double[] toDoubleArray(Collection<? extends Number> c) {
    double arr[] = new double[c.size()];
    int i = 0;//from   w  ww. j a v  a2  s  .  c o  m
    for (Number item : c) {
        arr[i++] = item.doubleValue();
    }
    return arr;
}

From source file:Main.java

/**
 * @param nums Any descendant of Number (Integer, Short, Double, Float, etc)
 * @return The mean of the number, or Double.NaN if the list is empty.
 *//*w ww.  ja v  a  2s . c om*/
public static double mean(Collection<? extends Number> nums) {
    if (nums == null || nums.isEmpty()) {
        return Double.NaN;
    }
    double sum = 0.0;
    for (Number n : nums) {
        sum += n.doubleValue();
    }
    return sum / nums.size();
}

From source file:Main.java

/**
 * @param coll {@link Collection} any collection object containing zero or more elements,can't be null.
 * @return {@link Number} the maximum value available in the collection.
 *//*from   w w w .ja v a2s.c om*/
public static Number max(Collection<? extends Number> coll) {
    Iterator<? extends Number> i = coll.iterator();
    Number candidate = i.next();
    while (i.hasNext()) {
        Number next = i.next();
        if (next.doubleValue() - candidate.doubleValue() > 0)
            candidate = next;
    }
    return candidate;
}

From source file:Main.java

public static double sum(Collection<? extends Number> collection) {
    double result = 0;
    for (Number number : collection) {
        result += number.doubleValue();
    }/*  ww w  . j a v  a2 s.  c o  m*/

    return result;
}

From source file:Main.java

public static double[] toDoubleArray(Collection<? extends Number> c) {
    double arr[] = new double[c.size()];
    int i = 0;/*from ww  w  .  j  a  v a  2 s.com*/
    for (Number item : c)
        arr[i++] = item.doubleValue();
    return arr;
}