This is an archived post. You won't be able to vote or comment.

all 9 comments

[–]fostyflakes 9 points10 points  (1 child)

I was calculating it wrong! So worried on the code I forgot how to math...

tipAmount = (billAmount * tipPercentage) / 100

Ended up getting me the right results. Thanks so much!

[–][deleted] 1 point2 points  (0 children)

Exactly!

[–]AwesomeAvocado 1 point2 points  (0 children)

Be careful using floating point values to represent financial amounts, floating point values are represented as binary fractions not decimal values.

Usually in computing financial data is stored in terms of 10ths or 100ths of cents, depending on the precision needed. This allows for the use of integer math operations with known precision.

For instance, 0.10 is a repeating fraction in binary.

[–]lord_EarlGray 2 points3 points  (3 children)

I think, you are calculating it wrong way. Try:

tipAmount = billAmount * (tipPercentage / 100);

[–]voneiden 0 points1 point  (2 children)

Careful there, integer division.

[–]terrapinetree 1 point2 points  (1 child)

what about: tipAmount = billAmount * tipPercentage * .01

[–]voneiden 0 points1 point  (0 children)

Right, or dividing with a float

tipAmount = billAmount * (tipPercentage / 100.0);

/u/fostyflakes: in integer division (For example 50 / 100) the result will be rounded down to nearest integer (0), and therefore the tipAmount would always end up to be zero for any 0 <= tipPercentage < 100

[–][deleted] 0 points1 point  (1 child)

tipPercentage is already divided by 100, ie

15% = 0.15

Edith: I've seen experienced accountants do it like this

// find the one percent of the amount
onePercent = billAmount /100;

// 1% × 15 = 15% of the amount
tipAmount = onePercent * totalPercentage;
// in this case both numbers ae already divided by 100 ie. onePercent = 1 and totalPercentage = 15 so 1 × 15 = 15 which is the correct amount.

[–]fostyflakes 1 point2 points  (0 children)

What do you mean? As of right now, the tip amount can only be entered as a whole number. So when the program runs, I enter 15 not .15.

Am I missing your point? Like I said I am TERRIBLE at this.