0

I am designing a cash register in which i have to compare 2 float values and i am getting an error every time while comparing it .i.e because i cannot round off the difference in the amount up to 2 decimal digits and compare it with values.

for eg. if the amount paid by the customer is 60.36 and the Bill issued is of 30.24 the difference coming out is 30.120001 . I even tried using floor function to round it off to two decimal digits : floorf(diff*100+.5)/100; but it doesn't work.

How should i round off the decimal digits and use them in designing a cash register??

3
  • You shouldn't compare floating point values. Commented Apr 10, 2013 at 7:07
  • Wrong, he shouldn't use floats for monetary values. Comparing them is fine unless you use ==. Commented Apr 10, 2013 at 7:08
  • 1
    Here's why (not to use floats), stackoverflow.com/questions/3730019/… Commented Apr 10, 2013 at 7:09

1 Answer 1

7

You shouldn't be using binary floating point to handle monetary amounts. There are many issues having to do with rounding and the inexact nature of floating point. For example, you won't be able to exactly represent 0.1 as a float.

Use fixed-point arithmetic instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Then how should i use perform calculations on float values like .01 or .05?
@user2227862 Use integer types to store the number of cents.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.