简体   繁体   中英

Is there a built in function for rounding a double in java

I want to round a double to a specific amount of decimal places in java. Is there a specific function for doing so and can this function round the double to an integer?

There is Math.round() that will return the rounded long value.

You can also cast a double value to a long or an int .

If you need to format the double value to a specific number of decimals then use DecimalFormat .

DateFormat df = new DateFormat("#.##");

The above statement can bring your double value to a specific format.

double d = Math.round(43.475);

The above statement can round your double value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM