aspose file tools*
The moose likes Beginning Java and the fly likes What is a better way to convert 5.6789 to 5.68? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is a better way to convert 5.6789 to 5.68?" Watch "What is a better way to convert 5.6789 to 5.68?" New topic
Author

What is a better way to convert 5.6789 to 5.68?

Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
Hi all,
the question is as my topic. I know there are a lot methods to do that in Java, but is there any class in Java which can directly do this format conversion?
Thanks a lot
Chris
Jody Seigle
Greenhorn

Joined: Sep 29, 2000
Posts: 24
The DecimalFormat class in the java.text package

double d = 5.6789;
DecimalFormat df = new DecimalFormat(0.00);
String output = df.format(d); // <- equals "5.68"
check the API for the description of the possible output formats.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521

You could multiply by 100.
Do a Math.round();
Divide by 100.


"JavaRanch, where the deer and the Certified play" - David O'Meara
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
thanks.
Yogen Vadnere
Ranch Hand

Joined: Sep 20, 2001
Posts: 58
I already posted following code in one of the forum,
import java.util.* ;
import java.text.* ;
public class test{
public static void main(String s[]){
double formatDouble = 750.1234 ;
NumberFormat converter = NumberFormat.getInstance() ;
converter.setMaximumFractionDigits(2);
converter.setMinimumFractionDigits(2);
System.out.println(converter.format(formatDouble));
}
}


Yogen Vadnere
Chris Ben
Ranch Hand

Joined: Jan 15, 2001
Posts: 135
It is always nice to know more.
Thank you
Chris
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is a better way to convert 5.6789 to 5.68?
 
Similar Threads
SCJP 1.6
Help for SCWCD 5
How to call class from different package?
how to open a pdf file in a new window from a jsp page
Couldn't find a destroy method named 'close' on bean with name 'dataSource'