Page 2
Convertir en palabras un número
Page 3
Index
See this article in english  Ver este artículo en español 
Page 4
Conversiones entre zonas horarias

Rounding a number to n decimal places

Alexander Hristov

The following function rounds any number to a specified number of decimal places. Of course, there are other ways of doing this in Java (using the BigInteger / BigDecimal classes for example), but if you don't need control over the rounding mode, this is an easy way to solve the problem:

 

 

Redondear.java
 
  public double redondear( double numero, int decimales ) {
    return Math.round(numero*Math.pow(10,decimales))/Math.pow(10,decimales);
  }

 


 

Comments

 

Add a Comment

Name (optional)
EMail (optional, will not be displayed)

Text