在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将有所帮助,但你可以看到我没有设法得到它的工作。

input和输出都是绝对必要的。

如果您更改上面的代码的第4行并将其发布,将会非常有帮助。

那么这个工程…

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

输出是

 123.14 

或者@Rufein说

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

这也会为你做。

  double d = 2.34568; DecimalFormat f = new DecimalFormat("##.00"); System.out.println(f.format(d)); 
 String roundOffTo2DecPlaces(float val) { return String.format("%.2f", val); } 
 BigDecimal a = new BigDecimal("123.13698"); BigDecimal roundOff = a.setScale(2, BigDecimal.ROUND_HALF_EVEN); System.out.println(roundOff); 

回到你的代码,并取代100 100.00 ,让我知道它是否工作。 但是,如果你想成为正式的,试试这个:

 import java.text.DecimalFormat; DecimalFormat df=new DecimalFormat("0.00"); String formate = df.format(value); double finalValue = (Double)df.parse(formate) ; 
 double roundOff = Math.round(a*100)/100; 

应该

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

将“D”加到100使其成双字面,因此产生的结果将具有精度

我知道这是一个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 

试试:

 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); }} 

看起来像你被整数算术命中:在某些语言(int)/(int)将始终被评估为整数算术。 为了强制浮点运算,确保至less有一个操作数是非整数:

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

这是一个长期的,但充分的certificate解决scheme,永远不会失败

只要把你的号码作为双精度函数传递给这个函数,它会返回你舍去十进制值到最接近的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.00; System.out.println(roundOff); } } 
 public static float roundFloat(float in) { return ((int)((in*100f)+0.5f))/100f; } 

大多数情况下应该可以。 如果你想兼容双打,你仍然可以改变types。