less than 1 minute read


In this tutorial, you will learn how to get the whole and fractional part of a number/float using the C++ programming language. For instance, given a number (float type) such as 51.21. The whole part would be 51 and the fractional part would be 0.21.

Method #1: Using std::modf

We can use the function std::modf to separate both parts of the number such as:

#include <iostream>
#include <cmath> //Important!

int main()
	float myFloat = 51.21;
	float wholePart, fractionalPart;
	fractionalPart = std::modf(myFloat, &wholePart);

	std::cout << wholePart << std::endl; //Returns 51
	std::cout << fractionalPart << std::endl; //Returns 0.21
	return 0;

NOTE: Do not forget to include the cmath library, otherwise we may get the std::modf is not a member of std error.


Read more about the std::modf function here: https://en.cppreference.com/w/cpp/numeric/math/modf