• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2691
  • Last Modified:

truncating a decimal no. in C++

I wanted to truncate a decimal no in c++
e.g
   
  #include <iostream>
  using namespace std;

 int main()
   {
     float b;
     b = 4.34534;
.I wanted to truncate b to "4".
0
chataholic4real
Asked:
chataholic4real
1 Solution
 
SteHCommented:
In this case you can use floor ():

  float btrunc = floor (b);

The more general case where you truncate to some digits after the decimal point:

 float btrunc = 1. / 1000 * floor ( b * 1000); // this is for 3 digits left replace 1000 with appropriate number for other number of digits.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now