Removing all numbers after a decimal point in C++
Go to solution
Solved by Glenwing,
11 minutes ago, Shammikit said:i have this number 2865 declared in a float variable. i want to convert this to 2.8 and remove all the numbers after this.
I have already tried codes like the ones shown below and they would result in either 2.865 or 2865 (no change). i want to get 2.8 only.this is probably something easy but i have been searching this for some time now and i havent really found anything.your help would be greatly appreciated.thank you
std::cout << std::floor(d * 100.) / 100. << std::endl; double truncated = (double)((int)dig*100)/100; double truncated = ("%.2f",m);
First code should work, but I think you have it backwards. You need to divide by 100, floor, and then divide by 10. That would go 2865 -> 28.65 -> 28.00 -> 2.8
Right now you have floor(2865 * 100) / 100 which would give you 2865 -> 286500 -> 286500 -> 2865.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now