Truncate a decimal value in C++

后端 未结 10 1666
萌比男神i
萌比男神i 2020-12-10 05:38

What\'s the easiest way to truncate a C++ float variable that has a value of 0.6000002 to a value of 0.6000 and store it back in the variable?

10条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-10 05:54

    i think the question that should be asked here is: Why do you need it truncated?

    If its for comparison between values, perhaps you should consider using the epsilon test. (with an extra tolerance value, in your case, since it seems to be far larger than the generally accepted epsilon).

    If you're just wanting to print it out as 0.6000 , use the methods others have suggested.

提交回复
热议问题