Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try <a href="http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html" rel="noreferrer"><code>java.text.NumberFormat</code></a>. From the Javadocs:</p> <blockquote> <p>To format a number for a different Locale, specify it in the call to getInstance. </p> <pre><code>NumberFormat nf = NumberFormat.getInstance(Locale.FRENCH); </code></pre> <p>You can also use a NumberFormat to parse numbers: </p> <pre><code>myNumber = nf.parse(myString); </code></pre> </blockquote> <p><a href="http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#parse(java.lang.String)" rel="noreferrer"><code>parse()</code></a> returns a <a href="http://java.sun.com/javase/6/docs/api/java/lang/Number.html" rel="noreferrer"><code>Number</code></a>; so to get a <code>double</code>, you must call <a href="http://java.sun.com/javase/6/docs/api/java/lang/Number.html#doubleValue()" rel="noreferrer"><code>myNumber.doubleValue()</code></a>:</p> <pre><code> double myNumber = nf.parse(myString).doubleValue(); </code></pre> <p>Note that <code>parse()</code> will never return <code>null</code>, so this cannot cause a <code>NullPointerException</code>. Instead, <code>parse</code> throws a checked <code>ParseException</code> if it fails.</p> <p><strong>Edit:</strong> I originally said that there was another way to convert to <code>double</code>: cast the result to <code>Double</code> and use unboxing. I thought that since a general-purpose instance of <code>NumberFormat</code> was being used (per the Javadocs for <a href="http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#getInstance(java.util.Locale)" rel="noreferrer"><code>getInstance</code></a>), it would always return a <code>Double</code>. But <a href="https://stackoverflow.com/users/19276/djclayworth">DJClayworth</a> points out that the Javadocs for <a href="http://java.sun.com/javase/6/docs/api/java/text/NumberFormat.html#parse(java.lang.String,%20java.text.ParsePosition)" rel="noreferrer"><code>parse(String, ParsePosition)</code></a> (which is called by <code>parse(String)</code>) say that a <code>Long</code> is returned if possible. Therefore, <strong>casting the result to <code>Double</code> is unsafe and should not be tried!</strong><br> <em>Thanks, DJClayworth!</em></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload