0

I have a one function i.e applyCredit(double amount )

if i am calling this function as applyCredit(inputAmount)

and inputAmount is in double 2 precision format i.e ####.00 if the amount in a function got updated several times

and now question is : format of the amount always will be with 2 precision or it may get changed as it is of the type double

Nutan
  • 778
  • 1
  • 8
  • 18
  • `inputAmount` cannot be in a specific format. Format is for display purposes only. A double doesn't store format information. – m0skit0 Aug 08 '16 at 09:17
  • So do u mean if we format the variable and try to save that it will not store in the format as given it will store the original value right ? – Nutan Aug 08 '16 at 09:19
  • 2
    Please take a look at [this](http://stackoverflow.com/a/21596413/6505091). That (`BigDecimal`) should do the trick, if you want to have control over the precision. – kaba713 Aug 08 '16 at 09:20
  • 1
    You should not use floating point numbers for currency, as some values can not be represented precisely and you may get odd rounding errors. Better use integer/long (in cents) – tobias_k Aug 08 '16 at 09:23
  • You don't "format the variable". Any formatting returns a string, not a double. Formatting is not applied inside a double. – m0skit0 Aug 08 '16 at 09:24
  • @m0skit0 after formatting i m type - casting that result to store – Nutan Aug 08 '16 at 09:25
  • @Nutan Sorry I don't understand what you mean with "typecasting that result to store". – m0skit0 Aug 08 '16 at 09:25
  • @m0skit0 type -casting mean i m converting that string into double and then storing in the database – Nutan Aug 08 '16 at 09:26
  • Why are converting back and forth from/to string to store? Why not store as double? – m0skit0 Aug 08 '16 at 09:32
  • @m0skit0 as it is the amount ...i m getting it in 5 precision and want to store it in 2 precision by doing the rounding of that 5 precision – Nutan Aug 08 '16 at 09:34

1 Answers1

0

As long as it can "fit in" the input can be with or without decimals

public static void main(String[] args) {
    System.out.println(applyCredit(2));
    System.out.println(applyCredit(2.66));
    System.out.println(applyCredit(2.72251));
}
static private double applyCredit(double amount ){
    return amount*2;
}

will print : 4.0 , 5.32 , 5.44502

Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24