Home > database >  Round float to 2 decimal places in C language?
Round float to 2 decimal places in C language?

Time:10-16

   float number = 123.8798831;
   
   number=(floorf((number   number * 0.1) * 100.0)) / 100.0;
   
   printf("number = %f",number);

I want to get number = 136.25

But the compiler shows me number = 136.259995

I know that I can write like this printf("number = %.2f",number) ,but I need the number itself for further operation.It is necessary that the number be stored in a variable as number = 136.25

CodePudding user response:

It is necessary that the number be stored in a variable as number = 136.25

But that would be the incorrect result. The precise result of number number * 0.1 is 136.26787141. When you round that downwards to 2 decimal places, the number that you would get is 136.26, and not 136.25.

However, there is no way to store 136.26 in a float because it simply isn't a representable value (on your system). Best you can get is a value that is very close to it. You have successfully produced a floating point number that is very close to 136.26. If you cannot accept the slight error in the value, then you shouldn't be using finite precision floating point arithmetic.

If you wish to print the value of a floating point number up to limited number of decimals, you must understand that not all values can be represented by floating point numbers, and that you must use %.2f to get desired output.

Round float to 2 decimal places in C language?

Just like you did:

  • multiply with 100
  • round
  • divide by 100

CodePudding user response:

I agree with the other comments/answers that using floating point numbers for money is usually not a good idea, not all numbers can be stored exactly. Basically, when you use floating point numbers, you sacrifice exactness for being able to storage very large and very small numbers and being able to store decimals. You don't want to sacrifice exactness when dealing with real money, but I think this is a student project, and no actual money is being calculated, so I wrote the small program to show one way of doing this.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void)
{
    double number, percent_interest, interest, result, rounded_result;

    number = 123.8798831;
    percent_interest = 0.1;
    interest = (number * percent_interest)/100; //Calculate interest of interest_rate percent.
    result = number   interest;
    rounded_result = floor(result * 100) / 100;


    printf("number=%f, percent_interest=%f, interest=%f, result=%f, rounded_result=%f\n", number, percent_interest, interest, result, rounded_result);

    return EXIT_SUCCESS;
}

As you can see, I use double instead float, because double has more precession and floating point constants are of type double not float. The code in your question should give you a warning because in

float number = 123.8798831;

123.8798831 is of type double and has to be converted to float (possibly losing precession in the process).

You should also notice that my program calculates interest at .1% (like you say you want to do) unlike the code in your question which calculates interest at 10%. Your code multiplies by 0.1 which is 10/100 or 10%.

CodePudding user response:

Here is an example of a function you can use for rounding to x number of decimals.

Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stddef.h>

double dround(double number, int dp)
{
    int charsNeeded = 1   snprintf(NULL, 0, "%.*f", dp, number);
    char *buffer = malloc(charsNeeded);
    snprintf(buffer, charsNeeded, "%.*f", dp, number);
    double result = atof(buffer);
    free(buffer);
    return result;
}

int main()
{

  float number = 37.777779;
  number = dround(number,2);

  printf("Number is %f\n",number);
  return 0;
}
  • Related