on 03-01-2008 9:45 AM
Hi Friends
I have a value like
1666.6666
I wnat to make the above value as below
1666.67
plz send me the solution.
Thanks
Hazrath.G
Hi
use one of the following
float val = 1666.6666;
val *= 100;
val = (float)Math.round(val)/100;
Or
DecimalFormat decimalFormat = new DecimalFormat( "#,###,###,##0.00" );
double val= 100.2397;
double newval = new Double(decimalFormat.format(val)).doubleValue();
Regards
Ayyapparaj
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Hazrat,
Use the following method in your class to round off the value.
use types as per your need as double or float.
private double round(double value, int decimalPlace)
{
double power_of_ten = 1;
while (decimalPlace-- > 0)
power_of_ten *= 10.0;
return Math.round(value * power_of_ten) / power_of_ten;
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Hazrat,
Here u go !!!!
Use the following code to round off to two places of decimal
import java.text.NumberFormat;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(2);
nf.setMinimumFractionDigits(2);
String formattedNo = nf.format(1666.6666);
formattedNo Shud contain the rounded value 1666.67
Note the the format returns String,you can convert the string accordingly.
Eg :For float use Float.parseFloat(formattedNo).
This should solve ur problem
Cheers,
Santhosh
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.