0
#include <stdio.h>

int main(void)
{
    

    double a= 10.12;
    int cents= (int)(a*100);
    printf("%d\n",cents);
    printf("%.15lf", a);
    return 0;
}

output:

1011
10.119999999999999
Barmar
  • 741,623
  • 53
  • 500
  • 612
Yamit058
  • 1
  • 2
  • 1
    Floating point can't represent `10.12` precisely, so the actual value is `10.119999999`. Use `round()` instead of `(int)` – Barmar May 20 '22 at 21:33
  • I think that's a floating point error, that, when converted to an integer, was rounded down. I found an explanation of floating point errors [here](https://blog.penjee.com/what-are-floating-point-errors-answered/) but didn't read it. – Electroboss May 20 '22 at 21:33
  • 1
    Consider reworking your code to something along the lines of `int a = 1012; int dollars = a / 100; int cents = a % 100; printf("$%d.%02d\n", dollars, cents);` – Steve Summit May 20 '22 at 21:41

0 Answers0