cancel
Showing results for 
Search instead for 
Did you mean: 

Regarding round the value.

Former Member
0 Kudos

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

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

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

Former Member
0 Kudos

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;

}

Former Member
0 Kudos

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