Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to implement roundup function in java

Tags:

java

math

How to achieve functionality similar to excel's Roundup() in java?

for example - how to round 0.3102 to 0.4 in java ?

like image 966
vkantiya Avatar asked Mar 07 '26 23:03

vkantiya


2 Answers

double r =0.3102;
BigDecimal bd = new BigDecimal(r);
bd = bd.setScale(1,BigDecimal.ROUND_UP);
r = bd.doubleValue();
System.out.println(r);
like image 140
stacker Avatar answered Mar 10 '26 13:03

stacker


BigDecimal can help in achieving this.

for example-

 // new BigDecimal(<your-number>).setScale(<scale-value>, BigDecimal.ROUND_UP)) 
 result =  new BigDecimal(0.3105).setScale(1, BigDecimal.ROUND_UP));

 // result = 0.4
like image 43
vkantiya Avatar answered Mar 10 '26 13:03

vkantiya