-6
float totalPurchaseAmount, totalPurchaseTax,totalShippingCharge, totalShippingTax,    totalGiftWrapCharge, totalGiftWrapTax, totalAmount;  
int totalAmountCents;

totalAmount = totalPurchaseAmount + totalPurchaseTax + totalShippingCharge + totalShippingTax + totalGiftWrapCharge + totalGiftWrapTax ;

/* The %.2f formatting string limits the output to 2 decimal places */

lr_output_message("total %.2f", totalAmount);

lr_output_message("total %.2f",totalAmount );o/p=totalAmount = 569.97

totalAmountCents = totalAmount * 100;

lr_output_message("total cents %f",totalAmountCents);

o/p=totalAmountCents=56996

(But I need to print 56997 but not 56996, I mean the exact value but as an int)

How do I change the program so it effectively change the float to int?

SiHa
  • 7,830
  • 13
  • 34
  • 43

3 Answers3

4

If you're writing a program for money that requires precision, don't use the floats in the first place. Store everything as an integer number of cents. Where floating point numbers (likely often double rather than float) are necessary - like an interest rate maybe - you'll need to put in rules that match the institution's rules, which may or may not be as simple as rounding.

Brick
  • 3,998
  • 8
  • 27
  • 47
0

Avoid changing the type used to represent money as done here between float and int.

float totalAmount;  
int totalAmountCents;
...
totalAmountCents = totalAmount * 100;

Money has special issues involving exactness, range and functions that do not simply apply to integers like complex tax and interest rate calculations. These issues are aggravated with casual type conversions.


For learning, start with int or long of the smallest unit of money and pay special attention to rounding.


If floating point type is required, rarely is float a good choice. Do not use integer conversion to round. Use rint() or round()

double totalAmount;  
double totalAmountCents;
...
totalAmountCents = rint(totalAmount * 100);

If a conversion to int is absolutely required, round using a floating point rounding function and then convert or use lrint().

#include <math.h>
totalAmountCents = rint(totalAmount * 100);
int iCents = (int) totalAmountCents;
// or 
long LCents = lrint(totalAmount * 100);
Community
  • 1
  • 1
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
-1

Add 0.5 to the float before casting it to an int.

It's what you would do in math too.

Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
  • This can fail to work due to the round-off error introduced when performing the addition. If you start with a number that happens to have no fractional part, for example, the result of adding 0.5 maybe a little above or a little below the next half-integer. – Brick Jan 09 '17 at 16:15
  • 2
    Adding 0.5 and then casting to `int` works for many `float` but generates the wrong answer for 1) many negative numbers, 2) the `float` value just below `0.5f`, 3) many large `float` values whose least significant bit is 0.5 or 1.0. – chux - Reinstate Monica Jan 09 '17 at 16:15
  • @chux true for 1) ... for the others it's not really the conversion that gives the problem, it's the fact float numbers never are exact in the last digit(s) ... casting it to an int will not make it better. – Michel Keijzers Jan 09 '17 at 16:18
  • 2
    @MichelKeijzers All finite `float` are _exact_. Its the operations that result in approximations. – chux - Reinstate Monica Jan 09 '17 at 16:22
  • .... using `(int) (some_float + 0.5)` turns out to not typically be a problem concerning [#2, #3](http://stackoverflow.com/questions/41551952/converting-float-to-int-effectively#comment70309613_41552006) because `0.5` is a double` and `double` math occurred. `(int) (some_float + 0.5f)` will not, as you [comment](http://stackoverflow.com/questions/41551952/converting-float-to-int-effectively#comment70309744_41552006) make it better, but can make things worse. C has various round functions `round(), rint(), nearbyint(), lrint()` that well handle these rounding issues. – chux - Reinstate Monica Jan 09 '17 at 17:25