I have a calculation that goes something like this:
Price = value * randomNumberBetween(decimalValueA, decimalValueB)
I was originally generating this using floats/doubles. However, after looking up a bit more on objective-c, it was mentioned numerous times that when calculating currency you should use NSDecimalNumber. The issue I have is that I use this 'price' variable in comparisons and things, for example:
if (deposit/price) < 0.2
return price*0.05;
Using NSDecimalNumber makes this a lot more difficult. As far as I'm aware I should be converting any magic numbers (in this case 0.2 and 0.05) to NSDecimalNumber so then I can compare them and use functions such as NSDecimalMultiply.
Also, if I have a function that is something like:
return (minRandomPercentage + ((maxRandomPercentage - minRandomPercentage) * (randomNumber)
it ends up becoming this ridiculous string of nested function calls like:
return [minRandomPercentage decimalNumberByAdding:[[maxRandomPercentage decimalNumberBySubtracting: minRandomPercentage] decimalNumberByMultiplyingBy:random]]
Is this seriously how objective-c deals with decimals? Can anyone give me any clues on how to make this a lot less arduous? I can live with the nested function calls if I could do comparisons with the result and not have to be casting every magic number I have.