kankan
asked on
java rounding problem
i want to ask someone,
how to round the decimal like this:
from 10.48 to 10.45
from 289.41 to 289.40 ???
ASAP
how to round the decimal like this:
from 10.48 to 10.45
from 289.41 to 289.40 ???
ASAP
This is a really weird rounding you are asking. Did you actualyl mean rounf the 10.48 to 10.50 instead? If yes below is a quick fix:
static BigDecimal format(BigDecimal n, int prec, int round)
{
return n.setScale(prec, round);
}
public static void main(String[] args)
{
BigDecimal bd = format(new BigDecimal("10.48"), 1, BigDecimal.ROUND_HALF_UP);
System.out.println(format( bd, 2, BigDecimal.ROUND_UNNECESSA RY));
bd = format(new BigDecimal("289.41"), 1, BigDecimal.ROUND_HALF_DOWN );
System.out.println(format( bd, 2, BigDecimal.ROUND_UNNECESSA RY));
}
If you indeed meant round the 10.48 to 10.45 I will have to put more thought in it. There is also a good tutorial on BigDecimal. It might come in handy:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
Hope it helps.
static BigDecimal format(BigDecimal n, int prec, int round)
{
return n.setScale(prec, round);
}
public static void main(String[] args)
{
BigDecimal bd = format(new BigDecimal("10.48"), 1, BigDecimal.ROUND_HALF_UP);
System.out.println(format(
bd = format(new BigDecimal("289.41"), 1, BigDecimal.ROUND_HALF_DOWN
System.out.println(format(
}
If you indeed meant round the 10.48 to 10.45 I will have to put more thought in it. There is also a good tutorial on BigDecimal. It might come in handy:
http://www.javaworld.com/javaworld/jw-06-2001/jw-0601-cents.html
Hope it helps.
The: new BigDecimal("10.48") can also be: new BigDecimal(10.48) as well.
Hope this helps,
private final double roundValue(double val)
{
java.math.BigDecimal bdVal = new java.math.BigDecimal(""+va l);
bdVal = bdVal.setScale(2, java.math.BigDecimal.ROUND _HALF_UP);
return bdVal.doubleValue();
}
private final double roundValue(double val)
{
java.math.BigDecimal bdVal = new java.math.BigDecimal(""+va
bdVal = bdVal.setScale(2, java.math.BigDecimal.ROUND
return bdVal.doubleValue();
}
Hi,
public static double roundValue(double val,int floating_point_digit_to_ro und ) {
double round_result;
double factor = Math.pow(10.0,(double)floa ting_point _digit_to_ round );
if(val>0) {
round_result = Math.floor(val*factor)+0.5 )/factor;
} else {
round_result = Math.ceil(val*factor)+0.5) /factor;
}
return round_result;
}
general function to round floating point
for your case use floating_point_digit_to_ro und = 1
This solution is more effiective (performance) than the other since you don't need to create temporary objects.
Best regards
Nir
public static double roundValue(double val,int floating_point_digit_to_ro
double round_result;
double factor = Math.pow(10.0,(double)floa
if(val>0) {
round_result = Math.floor(val*factor)+0.5
} else {
round_result = Math.ceil(val*factor)+0.5)
}
return round_result;
}
general function to round floating point
for your case use floating_point_digit_to_ro
This solution is more effiective (performance) than the other since you don't need to create temporary objects.
Best regards
Nir
ASKER
actually, no,
i just want
e.g. 14.48 it will be 14.45
round down to 5
or if 14.41 it will be 14.40
not rounding``
i just want
e.g. 14.48 it will be 14.45
round down to 5
or if 14.41 it will be 14.40
not rounding``
ASKER
actually, no,
i just want
e.g. 14.48 it will be 14.45
round down to 5
or if 14.41 it will be 14.40
not rounding``
i just want
e.g. 14.48 it will be 14.45
round down to 5
or if 14.41 it will be 14.40
not rounding``
you want >0.45 => 0.45 and <0.45 =>0.40 ?
ASKER
actually,
if 0.45 => 0.45
if 0.41 => 0.40
if 0.48 => 0.45
if 1.59 => 1.55
if 1.53 => 1.50
can u get it ??
ASAP
Thanks
if 0.45 => 0.45
if 0.41 => 0.40
if 0.48 => 0.45
if 1.59 => 1.55
if 1.53 => 1.50
can u get it ??
ASAP
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you mean 10.48 => 10.5
If need to round to the first digit after the floating point.
x (double poistive value or positive float)
x = Math.floor(x*10+0.5)/10.0;
for negtive use
x = Math.ceil(x*10-0.5)/10.0;
Hope it help
Best regards
Nir