Jump to content

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.

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);

 

Link to comment
Share on other sites

Link to post
Share on other sites

You can use this in C#:

 

//To string or something here
String.Format("{0:0.0}", 2.865);

 

Sorry, it's been awhile since I've used C++ but maybe you could find an equivalent . . .

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, TurbulentWinds2 said:

You can use this in C#:

 


//To string or something here
String.Format("{0:0.0}", 2.865);

 

Sorry, it's been awhile since I've used C++ but maybe you could find an equivalent . . .

didnt work here man. it output the same value

Link to comment
Share on other sites

Link to post
Share on other sites

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.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Glenwing said:

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.

yay!! that works. thank you

Link to comment
Share on other sites

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×