import java.math.BigDecimal;
import java.math.RoundingMode;
/**
* {@link <a href=
* "https://[Log in to view URL]"
* target="_blank">Arrotondamento condizionato sul decimale</a>}
*
* @author itammb ( Italia Massimiliano Buscati )
* @version JDK 1.15
*
*/
class Main {
/**
* @see BigDecimal
* @see BigDecimal#setScale(int newScale, RoundingMode roundingMode)
* @see RoundingMode
*
*/
public static void roundingOnDecimal() {
double calculated = 0.7d + 0.1d;
double calculatedOther = 0.9d - 0.1;
// arrotondamento con errore
System.out.println("0.7d + 0.1d -> " + calculated);
System.out.println("0.9d - 0.1 -> " + calculatedOther);
System.out.println(calculated + " != " + calculatedOther);
// arrotondamento condizionato sul decimale
BigDecimal bd = new BigDecimal(calculated);
// rounding policy: [cifra decimale, tipo arrotondamento]
bd = bd.setScale(1, RoundingMode.CEILING);
calculated = bd.doubleValue();
System.out.println(calculated + " == " + calculatedOther);
}
public static void main(String[] args) {
// Unit test - arrotondamento condizionato sul decimale
roundingOnDecimal();
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: