| Author |
Decimal digits
|
Abder-Rahman Ali
Ranch Hand
Joined: May 22, 2008
Posts: 138
|
|
In Java, when I do this for example: 0.75 - 0.25 I get: 0.5 How can I get: 0.50 Thanks. [ June 24, 2008: Message edited by: Abder-Rahman Ali ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24061
|
|
Realize that the answer, and how you display the answer, are two different things. Numbers don't have a certain number of digits; it's just how you display them that counts. Use the java.text.DecimalFormat class, or use the System.out.printf() method. Both are well- described in their Javadocs.
|
[Jess in Action][AskingGoodQuestions]
|
 |
vas reddy
Greenhorn
Joined: Jun 11, 2008
Posts: 22
|
|
try this... but final result is in String you need to convert that into double import java.text.*; public class Value { public static void main(String args[]) { double x = 0.75; double y = 0.25; double z = x-y; System.out.println("z =" + z); NumberFormat nf = NumberFormat.getInstance(); nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); String s = nf.format(z); System.out.println("s =" + s); } }
|
 |
 |
|
|
subject: Decimal digits
|
|
|