FruitPunchSamurai FruitPunchSamurai - 18 days ago 6
C Question

how do you truncate instead of rounding? C

I have a program that returns float numbers.I need to return the answer with exactly two digits after the decimal place and truncate any extra digits rather than rounding. Can someone give me an example please?

Answer

There is a special function for truncation: trunc(..) which discards everything after a decimal point.

If you want to truncate after a certain number of decimal digits, you can do something like following:

trunc(number * 100) / 100

It should be obvious how above line works.