I'm new to java programming. I would like to round up a price to the nearest 2 decimal places with a multiple of 5.
Eg.
38.80
stays the same.38.81
to38.80
.38.82
to38.80
.38.83
to38.85
.38.84
to38.85
.38.85
stays the same.38.86
to38.85
.38.87
to38.85
.38.88
to38.90
.38.89
to38.90
.38.90
stays the same.
I tried the provided duplicates but they come out only to 1 decimal place.
Eg. 38.82
to 38.8
.
This is my code:
import java.text.DecimalFormat;
public class RoundUp {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.##");
double num = 38.84;
System.out.println(df.format(Math.round(num*10.00)/10.00));
}
}
I have looked into other model answers by experts in this web but none of them really answer my question. Setting into 2 decimal places, I'm using DemicalFormat
. That I know, but rounding the number, let's say 38.83 to 38.85 and 38.87 to 38.90 is what I really want.
It is a rounding system that my country is using. Can check it out here.
**
This question has been answer by @Mureinik double rounded = Math.round(num * 100.0 / 5.0) * 5.0 / 100.0;
**