Rounding price number in java -
this question has answer here:
i'm new java programming. round price nearest 2 decimal places multiple of 5.
eg.
38.80stays same.38.8138.80.38.8238.80.38.8338.85.38.8438.85.38.85stays same.38.8638.85.38.8738.85.38.8838.90.38.8938.90.38.90stays 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
Post a Comment