I want to increase value of 6.23 to 6.25 in java. Right now I am planning to convert double to string than use split and take last two decimal digits and convert them into double. than use if() condtions. Any function in java which can do this for me? Or any other way?
Asked
Active
Viewed 159 times
0
-
7How about `6.23 + 0.02`? – adarshr Dec 04 '12 at 09:30
-
I want to do this operation on currency values.6.26 will change to 6.30 – TechHelp Dec 04 '12 at 09:30
-
does 6.23 + 0.02 not working for you? – Pradeep Simha Dec 04 '12 at 09:30
-
1Are you asking how to round currency values up to the next highest multiple of five cents? – Dawood ibn Kareem Dec 04 '12 at 09:31
-
1Are you trying to round to the nearest multiple of 0.05? E.g., 6.22 should be rounded to 6.20? – rodion Dec 04 '12 at 09:32
-
related: http://stackoverflow.com/questions/522855/how-do-i-round-up-currency-values-in-java – Dec 04 '12 at 09:33
-
There are ceil and floor but it will convert it to 6.20 or if you do 6.23+0.05 then it will go on to make 6.28 and then ceil will give you 6.3, What is your exact requirement? – Anoop Vaidya Dec 04 '12 at 09:34
-
yes i want to round currency value up to next highest multiple of 5 cents – TechHelp Dec 04 '12 at 09:35
-
if value of is 6.22 than it will convert to 6.20 and if it is 6.23 than it will roundup to 6.25 – TechHelp Dec 04 '12 at 09:37
3 Answers
1
If you want to do operations on currency without precision/rounding errors, you can use BigDecimal
. In order to round to a whole value you must call divide()
and set the scale
parameter to 0...
BigDecimal precision = new BigDecimal("0.05")
BigDecimal value = new BigDecimal("6.23");
BigDecimal rounded = value.divide(precision, 0, BigDecimal.ROUND_UP).multiply(precision);

Zutty
- 5,357
- 26
- 31
-
6.23 is just an example My program is generating many vaules similar to this. this method may not work for me. – TechHelp Dec 04 '12 at 09:45
-
Why not? You can cast the value to String by calling `String.valueOf(6.23)` – Stanley Dec 04 '12 at 10:04
1
double value = 6.21;
value = value * 100;
value = Math.round((value + 2.5)/ 5.0) * 5.0;
value = value / 100;
System.out.println(value);

Stanley
- 5,057
- 4
- 34
- 44
-
1
-
Thanks Stanley it is working fine but is it possible to make 6.21 and 6.22 values to 6.20 – TechHelp Dec 04 '12 at 09:49
-
Agree with @assylias. You can refer to assylias's answer. It does exactly what you need. – Stanley Dec 04 '12 at 09:53
1
You can divide by 0.05 (your precision step), round to the nearest integer, and multiply by 0.05 again:
BigDecimal precision = new BigDecimal("0.05");
BigDecimal value = new BigDecimal("6.23");
BigDecimal rounded = value.divide(precision, 0, RoundingMode.HALF_DOWN)
.multiply(precision);
System.out.println("rounded = " + rounded); //prints 6.25
You can then extract that in a utility method:
public class Test {
private static final BigDecimal PRECISION = new BigDecimal("0.05");
public static void main(String[] args) {
BigDecimal value = new BigDecimal("6.23");
System.out.println("value = " + value + " / rounded = " + roundCurrency(value));
value = new BigDecimal("6.22");
System.out.println("value = " + value + " / rounded = " + roundCurrency(value));
value = new BigDecimal("6.20");
System.out.println("value = " + value + " / rounded = " + roundCurrency(value));
value = new BigDecimal("6.19");
System.out.println("value = " + value + " / rounded = " + roundCurrency(value));
}
public static BigDecimal roundCurrency(BigDecimal value) {
return value.divide(PRECISION, 0, RoundingMode.HALF_DOWN)
.multiply(PRECISION);
}
}

assylias
- 321,522
- 82
- 660
- 783