Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Apr 12, 2002

Set Fraction Digits


To reduce the number of fraction digits in a double type variable, follow the code sample below. This can be useful in money calculations. Firstly, create a NumberFormat instance and set the maximum number of fraction digits you want. Next, pass your double variable to the format method of the NumberFormat class as an argument. It will return a formatted string.
 
import java.text.*;

public class setdouble 
{
	public static void main(String[] args) 
	{
		double number = 2.5434454554;
		NumberFormat nf = 
NumberFormat.getInstance();
		nf.setMaximumFractionDigits(2);
		System.out.println(nf.format(number));
	}
}
Shaik Chanbasha
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date