Rounding price number in java -


i'm new java programming. round price nearest 2 decimal places multiple of 5.

eg.

  • 38.80 stays same.
  • 38.81 38.80.
  • 38.82 38.80.
  • 38.83 38.85.
  • 38.84 38.85.
  • 38.85 stays same.
  • 38.86 38.85.
  • 38.87 38.85.
  • 38.88 38.90.
  • 38.89 38.90.
  • 38.90 stays same.

i tried provided duplicates come out 1 decimal place. eg. 38.82 38.8.

this 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 other model answers experts in web none of them answer question. setting 2 decimal places, i'm using demicalformat. know, rounding number, let's 38.83 38.85 , 38.87 38.90 want. rounding system country using. can check out here.

**

this question has been answer @mureinik double rounded = math.round(num * 100.0 / 5.0) * 5.0 / 100.0;

**

i recommend use bigdecimal instead of double when dealing money.

and like

bigdecimal value = new bigdecimal(38.84); value = value.setscale(2, bigdecimal.round_half_up) 

you can refer javadocs round_half_up , setscale


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -