-3

I have number 0.2289 and i want round down it to make 0.2200 Can i help pls ?

i tried printf(%.2f/n) but it rounds it up

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
Zidane
  • 1
  • 2
    I guess you could multiply it by 100.0f, then take the floorf() of that, then divide by 100.0f. – pmacfarlane Nov 06 '22 at 21:39
  • 1
    Beware that while you can certainly do this, after you're done you are likely to end up with a number like 0.2199999988 or 0.22000000000000000111, because 0.22 cannot be represented exactly in binary. – Steve Summit Nov 06 '22 at 22:02

1 Answers1

0

Usually you will not have an exact representation of the rounded number but you can try to:

double roundDown2(const double number)
{
    return floor(number * 100) / 100;
}


int main(void)
{
    printf("%.20f\n", roundDown2( 0.2289));
}

https://godbolt.org/z/5a9EshWM1

0___________
  • 60,014
  • 4
  • 34
  • 74