Rounding UP double values in java based on parameters -


i have following scenario , let assume 125,250,500 rounding parameter based on have rounding.

  1. when rounding parameter 125 need round off 1/8 th dollar, ie, value = 10.01 should return 10.125 value = 110.2 should return 110.250

  2. when rounding parameter 250 need round off quarter dollar, ie, value = 10.01 should return 10.250 value = 110.3 should return 110.500

  3. when rounding parameter 500 need round off half dollar, ie, value = 10.01 should return 10.500 value = 110.6 should return 111.

i have written code using math.round(8f * value/8f) rounds of nearest 1/8 th dollar rounding should in case 10.01 rounds off 10

first off, you're not rounding, you're doing ceiling.

this code want:

math.ceil(n * 1000 / factor) * factor / 1000; 

here code bundled method:

public static double ceil(double n, int factor) {     return math.ceil(n * 1000 / factor) * factor / 1000; } 

note no casting required, because in java arithemtic if 1 of operands double, others automatically (and safely) widened double too.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -