在java中四舍五入到小数点后2位?

我读了很多stackoverflow的问题,但似乎没有一个对我有用。我使用math.round()来舍入。 代码如下:

class round{
public static void main(String args[]){


double a = 123.13698;
double roundOff = Math.round(a*100)/100;


System.out.println(roundOff);
}
}

我得到的输出是:123,但我想它是123.14。我读到,添加*100/100将有所帮助,但如你所见,我没有设法让它工作。

输入和输出都是double是绝对必要的。

如果您更改上面代码的第4行并发布它,将会有很大的帮助。

1216074 次浏览
BigDecimal a = new BigDecimal("123.13698");
BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN);
System.out.println(roundOff);
回到你的代码,用100.00替换100,并让我知道它是否有效。 但是,如果你想要正式一点,可以试试这个:

import java.text.DecimalFormat;
DecimalFormat df=new DecimalFormat("0.00");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
     double d = 2.34568;
DecimalFormat f = new DecimalFormat("##.00");
System.out.println(f.format(d));

看起来你被整数算术击中了:在某些语言中(int)/(int)总是被计算为整数算术。 为了强制浮点运算,请确保至少有一个操作数是非整数:

double roundOff = Math.round(a*100)/100.f;
double roundOff = Math.round(a*100)/100;

应该是

double roundOff = Math.round(a*100)/100D;

将'D'添加到100使其成为双字面量,因此产生的结果将具有精度

String roundOffTo2DecPlaces(float val)
{
return String.format("%.2f", val);
}

好吧,这个可行……

double roundOff = Math.round(a * 100.0) / 100.0;

输出是

123.14

或者像@Rufein说的那样

 double roundOff = (double) Math.round(a * 100) / 100;

这也会对你有帮助。

我只是修改了你的代码。它在我的系统中工作得很好。看看这是否有用

class round{
public static void main(String args[]){


double a = 123.13698;
double roundOff = Math.round(a*100)/100.00;


System.out.println(roundOff);
}
}
public static float roundFloat(float in) {
return ((int)((in*100f)+0.5f))/100f;
}

在大多数情况下应该没问题。例如,如果您希望与double类型兼容,您仍然可以更改类型。

我知道这是一个2年前的问题,但每个人在某个时间点都面临着一个问题。我想分享一种不同的方法,通过使用BigDecimal类,可以为我们提供任意尺度的四舍五入值。在这里,我们可以避免使用DecimalFormat("0.00")Math.round(a * 100) / 100获得最终值所需的额外步骤。

import java.math.BigDecimal;


public class RoundingNumbers {
public static void main(String args[]){
double number = 123.13698;
int decimalsToConsider = 2;
BigDecimal bigDecimal = new BigDecimal(number);
BigDecimal roundedWithScale = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("Rounded value with setting scale = "+roundedWithScale);


bigDecimal = new BigDecimal(number);
BigDecimal roundedValueWithDivideLogic = bigDecimal.divide(BigDecimal.ONE,decimalsToConsider,BigDecimal.ROUND_HALF_UP);
System.out.println("Rounded value with Dividing by one = "+roundedValueWithDivideLogic);


}
}

这个程序会给我们以下的输出

Rounded value with setting scale = 123.14
Rounded value with Dividing by one = 123.14

这是一个很长的解决方案,但完全证明,从来不会失败

只要把你的数字作为双精度值传递给这个函数,它就会返回你四舍五入到十进制值的最近值5;

如果4.25,输出4.25

如果4.20,输出4.20

如果4.24,输出4.20

如果4.26,输出4.30

如果你想四舍五入到小数点后2位,那么使用

DecimalFormat df = new DecimalFormat("#.##");
roundToMultipleOfFive(Double.valueOf(df.format(number)));

如果最多3个位置,新建DecimalFormat("#.###")

如果多达n个位置,则新的DecimalFormat("#.nTimes #")

 public double roundToMultipleOfFive(double x)
{


x=input.nextDouble();
String str=String.valueOf(x);
int pos=0;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='.')
{
pos=i;
break;
}
}


int after=Integer.parseInt(str.substring(pos+1,str.length()));
int Q=after/5;
int R =after%5;


if((Q%2)==0)
{
after=after-R;
}
else
{
if(5-R==5)
{
after=after;
}
else after=after+(5-R);
}


return Double.parseDouble(str.substring(0,pos+1).concat(String.valueOf(after))));


}

试一试:

class round{
public static void main(String args[]){


double a = 123.13698;
double roundOff = Math.round(a*100)/100;
String.format("%.3f", roundOff); //%.3f defines decimal precision you want
System.out.println(roundOff);   }}