Tuesday, November 23, 2021

JAVA CODES Memories

1. How To Formatting A Decimal Number to 2 Decimal Places 

Define:

Double d

d = 4.587;

To set d to 2 decimal places, use one of the following functions:

1. String sDecNum = String.format("%.2f",d));

2. Formatter formatter = new Formatter(); String sDecNum = formatter.format("%.2f", d);

Library : import java.util.Formatter;

3. DecimalFormat df=new DecimalFormat("#.##");    String sDecNum = df.format(d);

Library : import java.text.DecimalFormat;