truncating a decimal no.  in C++

Posted on 2004-11-10
Last Modified: 2013-11-17
I wanted to truncate a decimal no in c++
  #include <iostream>
  using namespace std;

 int main()
     float b;
     b = 4.34534;
.I wanted to truncate b to "4".
Question by:chataholic4real
    1 Comment
    LVL 13

    Accepted Solution

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now