java rounding off with decimals

java rounding off with decimals

// method one:
double f = 3.1516;
BigDecimal b = new BigDecimal(f);
double f1 = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
 
// Method 2:
new java.text.DecimalFormat("#.00").format(3.1415926);
// #.00 means two decimal places #.0000 four decimal places and so on...
 
// Method three:
double d = 3.1415926;
String result = String.format("%.2f", d);
// %.2f %. Indicates any number of digits before the decimal point. 2 Indicates two decimal places. The result after the format is f represents a floating point type.
 
//Method four:
Math.round(5.2644555 * 100) * 0.01d;
//String.format("%0" + 15 + "d", 23) If 23 is less than 15, add 0 in front

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485224&siteId=291194637