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));	}}
Share the Post:
Share on facebook
Share on twitter
Share on linkedin


Recent Articles: