-1

I am a beginner in android. I am developing a Sales related app in which I need to do lots of calculations. I am confused which Variables to use for processing Prices, Discounts etc.

Currently I am storing Price as INTEGER. E.g. 180 Rs ---> 18000 Paise & while Showing I simple divide it by 100.

But I want to show price/discounts upto 2 decimal places like 150.50 Rs or 20.50Rs. What should I use? A float Or A Double or Anything else? Please help me out... Thanks...

AnujDeo
  • 333
  • 1
  • 4
  • 13
  • 1
    Have you even tried to Google the difference of the three? Please read the [faq]. – TronicZomB May 15 '13 at 12:46
  • Please read up on floating points: http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html. You will encounter problems with rounding. – RvdK May 15 '13 at 12:55
  • 2
    More bold font could help your question stand out more... – vikingsteve May 15 '13 at 13:05
  • For dealing with money, always use BigDecimal. Nothing else. http://developer.android.com/reference/java/math/BigDecimal.html Number of decimal places doesn't depend on the data type, but formatting: http://stackoverflow.com/questions/3395825/how-to-print-formatted-bigdecimal-values – NeplatnyUdaj May 15 '13 at 12:50
  • Care to reason why to use BigDecimal? – WarrenFaith May 15 '13 at 12:54
  • If you use floats, you're losing precision. If you use integers, you cannot use fractions. – NeplatnyUdaj May 15 '13 at 12:56
  • The smallest integer that's not exactly representable by a float is 16777217, and the smallest integer that's not exactly representable by a double is 9007199254740993. You'd have to be balancing Apple's books before you got a loss of precision from using floating-point. – Dan Hulme May 15 '13 at 12:59
  • I personally would go with integers and would only divide by 100 to display it... – WarrenFaith May 15 '13 at 13:02
  • @DanHulme Using doubles and rounding it is okay if you just count a few numbers. But if you do a lot of microtransactions or if you do some complicated calculations, you can very get into trouble quickly. – NeplatnyUdaj May 15 '13 at 13:10
  • If you only use numbers that can be exactly represented by the floating-point representation you're using, there won't be any problems. Sure, people need to think carefully about using fp, but rounding errors don't magically appear from nowhere, and superstitious thinking won't make things any easier. – Dan Hulme May 15 '13 at 13:32
  • @WarrenFaith... Thnks for suggestion. I have no problems for price with above format. But consider this case: price=7500 paise & Discont=25% i.e. 1875 Paise. So discounted amount will be 5625 Paise.. dividing it will show 56 Rs. Thats why i was confused regarding formatting. And I just wanted to know the correct way for it. Thanks for your help.. :-) – AnujDeo May 16 '13 at 07:54

1 Answers1

1

Use integers as you are doing to make calculations on your prices: you'll avoid problems with rounding. Just divide by 100 when you want to convert to real prices. And use a java.text.NumberFormat each time you want to show the price: you'll control the exact number of decimals it'll print.

eternay
  • 3,754
  • 2
  • 29
  • 27
  • @etetnay.. Yes I have no problems for price with above format. But consider this case: price=7500 paise & Discont=25% i.e. 1875 Paise. So discounted amount will be 5625 Paise.. dividing it will show 56 Rs. Thats why i was confused regarding formatting. And I just wanted to know the correct way for it. Thanks for your help.. :-) – AnujDeo May 16 '13 at 07:43
  • And in some cases... When I use Float to store Discounted amount, It gives answer as 60.000001 Rs... I wanted it to be 60.0 – AnujDeo May 16 '13 at 07:51
  • In java, division between 2 integers gives an integer, so it's normal to have 56 in this case. Just cast the 2 integers as float (or double, but you don't need the additional precision in that case) and put the result in a float (or a double, if you prefer). It'll give you the correct result (56.25). – eternay May 16 '13 at 08:33
  • In the case of a result of 60.000001, you have 2 solutions: just keep only 2 digits in the decimal part of the number and round it or use `BigDecimal` as @NeplatnyUdaj told you. – eternay May 16 '13 at 08:40
  • Thanks a lot.... I will let you know soon... thanks – AnujDeo May 16 '13 at 10:16
  • I found this [link](http://www.mkyong.com/java/how-to-round-double-float-value-to-2-decimal-points-in-java/) ...According to this post I found 2 solutions... 1) Math.round(double*100.0)/100.0 & 2) DecimalFormat(“###.##”) – AnujDeo May 16 '13 at 10:22