How to store float variable into another variable

Suppose a = 3.91900007534.
In C language, I want to store the value of the variable a in another variable, r, up to two decimal places, such that r = 3.92.
Note that I don’t want to print the value up to two decimal places, I just want to store the value as I need the exact value of r for the next operation.**

I haven’t used C/C++ in quite a while so I’m assuming there is no rounding function provided by any of the default libraries that will help you do this?

One way you can do this manually is:

  • multiply the original number by 100
  • add 0.5 to that product
  • truncate any decimals so you are left with just an integer
  • now divide that integer by 100

You should now have the original number rounded to two decimals.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.