Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><code>Double</code> doesn't have any concept of maintaining insignificant digits.</p> <p>The problem isn't the <code>Double</code> (or <code>double</code>) value you're using - it's how you then convert it <em>back</em> into a string. You should use a <a href="http://docs.oracle.com/javase/7/docs/api/java/text/DecimalFormat.html" rel="nofollow noreferrer"><code>DecimalFormat</code></a> which specifies the appropriate number of decimal places.</p> <p>Also note that you're currently using <code>Double.parseDouble</code> which returns a <code>double</code>, but you're assigning it to a <code>Double</code> variable, which will box the value - potentially unnecessarily.</p> <p>Sample code:</p> <pre><code>import java.text.DecimalFormat; public class Test { public static void main(String[] args) { String text = "500.00"; double number = Double.parseDouble(text); DecimalFormat format = new DecimalFormat("0.00"); String formatted = format.format(number); System.out.println(formatted); } } </code></pre> <p>Note that the above code will give you two decimal digits even if there are <em>more</em> digits available.</p> <p>Additionally, if exact decimal digits matter to you, you might want to consider using <code>BigDecimal</code> instead of <code>double</code>. This is particularly important if your values actually represent currency. You should understand the difference between a floating <em>binary</em> point type (such as <code>double</code>) and a floating <em>decimal</em> point type (such as <code>BigDecimal</code>) and choose appropriately between them.</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